Eugene
Eugene

Reputation: 1

MailApp.sendEmail =: validate the input email availability from user before send email through MailApp

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

Answers (2)

Iamblichus
Iamblichus

Reputation: 19339

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:

Upvotes: 1

arul selvan
arul selvan

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

Related Questions