Skip to content
Advertisement

Checking if an email is valid in Google Apps Script

I’m using the built-in api for scripting against Google Spreadsheets to send some booking confirmations, and currently my script breaks if someone has filled in an invalid email. I’d like it to just save some data to a list of guests that haven’t been notified, and then proceed with looping through the bookings.

This is my current code (simplified):

// The variables email, subject and msg are populated.
// I've tested that using Browser.msgBox(), and the correct column values are
// found and used

// The script breaks here, if an incorrect email address has been filled in
MailApp.sendEmail(email, subject, msg)

According to the documentation the only two methods on the MailApp class are to send emails and check the daily quota – nothing about checking for valid email addresses – so I don’t really know what criteria must be fulfilled for the class to accept the request, and thus can’t write a validation routine.

Advertisement

Answer

Stay calm, catch and log the exception and carry on:

try {
  // do stuff, including send email
  MailApp.sendEmail(email, subject, msg)
} catch(e) {
  Logger.log("Error with email (" + email + "). " + e);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement