Reputation: 1
Currently i am developing a google form that allow user to key in the recipient email and then send out email after submit the form.
Is there anyway to validate the input email availability from user before send email through MailApp?
I try MailApp.sendEmail(recipient, subject, body, {noReply:true,htmlBody_message, replyTo:user email})
whenever user key in wrong recipient email, system still send back the notification to me(admin) rather than replyTo user's email.
Need help..> <'
Eugene
Upvotes: 0
Views: 161
Reputation: 19339
If you want to look for emails in your organization, you can use Users: get to check if the email corresponds to any account in your organization.
To do this, you would have to enable Admin SDK Directory Service, and use, for example, this:
const existsInDomain = email => {
try {
const res = AdminDirectory.Users.get(email);
return true;
} catch(error) {
return false;
}
}
If you want to check this for email addresses not in your organization, the situation becomes more complicated. You could check if the email address is properly formatted using Regex, as in some of the answers to this question, but I don't know of any way to programmatically check if a specific email address exists (there seem to exist some online tools, but no idea if there is a programmatic way you can integrate to your code).
Upvotes: 1
Reputation: 624
Try this
function isValidEmailInMyDomain(address) {
var parts = address.split('@');
if( parts.length != 2 )
return false;
if( parts[1] != UserManager.getDomain() )
return false;
try {
UserManager.getUser(parts[0]);
return true;
} catch(doesNotExist) {
return false;
}
}
function testFunction() { //check the menu View > Logs
Logger.log(isValidEmailInMyDomain('[email protected]'));
}
Upvotes: 0