在Google Apps脚本中检查电子邮件是否有效

问题描述:

我正在使用内置的API来针对Google Spreadsheets发送脚本来发送预订确认信息,而且如果有人填写了无效的电子邮件,我的脚本就会中断。我希望它能将一些数据保存到未通知的客人列表中,然后继续循环预订。

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)

根据文档 MailApp上只有两种方法类将发送电子邮件并检查每日配额 - 没有任何关于检查有效的电子邮件地址的信息 - 所以我不知道为了接受请求必须满足什么标准,你可以写一个验证例程。

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.

保持冷静,抓住并记录异常并继续:

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