我正在尝试运行脚本将自动电子邮件发送到人员列表(我需要编辑一些信息)。
function sendEmail(){
for (var key in emailBank){
var email = getEmail(key);
var bodyText = emailBank[key]['body'];
var commitmentType = emailBank[key]['type'];
GmailApp.sendEmail(key,"Subject Line", bodyText {htmlBody:bodyText});
}
}
我正在使用数组(emailBank)在发送之前存储电子表格中的信息。该函数应循环并向每个人发送电子邮件。
有时候,我收到以下错误:“很抱歉,发生了服务器错误。请稍等一下再试一次。”包括GmailApp在内的该行。
您能否考虑验证是否发送了电子邮件?我担心的是,一半的电子邮件将被发送,一半不会,所以我不知道谁实际收到了电子邮件,并且循环停止了。
这里有任何想法!
答案 0 :(得分:1)
好的,所以我想在尝试了几种方法后找到了解决方法:
// Variables for error checking later
var emailRecipients = "";
var trueRecipients = "";
//The sendEmail function to send the email as expected
//It will also increment the emailRecipients variable
//only after the email has been sent
function sendEmail(){
for (var key in emailBank){
var email = getEmail(key);
var bodyText = emailBank[key]['body'];
var commitmentType = emailBank[key]['type'];
GmailApp.sendEmail(key,"Subject Line", bodyText {htmlBody:bodyText});
emailRecipients += key + "<br>";
}
}
//This function will only run if there was an error
//It will check increment all of the email addresses
//that should have been sent a message and then send me an
function errorEmail(){
for (var key in emailBank){
trueRecipients += key + "<br>";
}
var errorBodyText = "Emails sent to:<br><br>" + emailRecipients + "<br><br>Number of emails that should have sent:<br><br>" + trueRecipients;
GmailApp.sendEmail("example@gmail.com","Email Errors", errorBodyText,{htmlBody:errorBodyText});
}
function reminderEmail(){
try{
sendEmail();
}
catch(e){
errorEmail();
}
}
答案 1 :(得分:0)
作为临时解决方案,您可以在调用sendEmail()方法后查看上次发送的电子邮件。
/* the search() method returns an array of GmailThread objects.
Pop() returns the GmailThread */
var threads = GmailApp.search("label: sent", 0, 1).pop(); // returns the most recent thread in 'Sent Mail'
var msg = threads.getMessages().pop(); //returns the most recent message
Logger.log(msg.getId()); //returns unique id for the message
Logger.log(msg.getTo()); //returns comma-separated list of recipients
一个选项是存储消息ID并检查它是否在每次循环运行时发生变化。您也可以直接比较电子邮件属性,例如收件人的电子邮件,与您发送的邮件的属性相对应。