Google Script:MailApp.sendEmail到多个地址?

时间:2015-02-05 22:21:21

标签: email google-apps-script

我有一个使用以下脚本的脚本:

MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

但是,我想将同样的邮件发送给row.shiftSupervisor,这可能是我所忽略的非常简单的事情,但我想这里有人会立即知道它是什么。

为你的帮助干杯:)

修改 - 我尝试使用:

MailApp.sendEmail(row.shiftManager, row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
      row.state = STATE_PENDING;

但没有快乐。

编辑2 - 我使用了它:

  MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  MailApp.sendEmail(row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

不是最优雅的代码片段,但它完成了工作......

编辑3 - 在查看Sandy的解决方案后,我发现它是格式化的。 桑迪'解决方案工作正常,但导致与我的脚本的其他部分冲突。所以我的解决方案是:

MailApp.sendEmail(row.shiftManager + "," + row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});

2 个答案:

答案 0 :(得分:17)

一种解决方案是以这种方式配置语法:

MailApp.sendEmail(row.shiftManager + "," + row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});

另一种方法是先将多个电子邮件地址放入变量中,然后使用以下语法:

MailApp.sendEmail({
  to: recipientsTO,
  cc: recipientsCC,
  subject: Subject,
  htmlBody: html
});

完整的代码是:

function sendToMultiple() {
  var message = "This is a test of HTML <br><br> Line two";

  var recipientsTO = "example@gmail.com" + "," + "example@yahoo.com";
  var recipientsCC = "example@gmail.com";
  var Subject = "Holiday Approval Request";
  var html = message;

  MailApp.sendEmail({
    to: recipientsTO,
    cc: recipientsCC,
    subject: Subject,
    htmlBody: html
  });

}

该链接的示例中显示了该语法:

Google Documentation - MailApp.sendEmail

答案 1 :(得分:0)

这是我的做法:

//check quota and log
const emailsLeft = MailApp.getRemainingDailyQuota();
console.log( emailsLeft + " emails left in quota");

//get list of emails from spreadsheet itself
//filter out empty rows
const emails = getTab("Config").getRange("D2:D").getValues().map(function(el){ return el[0]; }).filter(function(el){ return el != '' });  

//send emails from NO_REPLY email, subject and HTML body
MailApp.sendEmail({
  to: emails.join(","),
  subject: subject,
  htmlBody: html,
  noReply: true
});

getTab()和其他辅助函数可以在这里找到 https://github.com/tim-kozak/google-spreadsheet-helpers