0

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

Cooper
  • 59,616
  • 6
  • 23
  • 54
Eugene
  • 1

2 Answers2

1

Email addresses in your organization:

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;
  }
}

Other email addresses:

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).

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Hi lamblichus, your idea is to compare with directory right? – Eugene Apr 20 '20 at 09:07
  • @Eugene Not sure what you're asking for here. If you want to know whether the email address exists in your organization, use the first method in my answer. – Iamblichus Apr 20 '20 at 12:12
0

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('aEmailIn@yourDomain.com'));
}

arul selvan
  • 616
  • 4
  • 17