3

使用 sendEmail,如何通过组合两个表单字段向多个逗号分隔的收件人发送电子邮件?当 (lastrow,4) 只有一个值 (abc@domain.com) 但不超过一个 (abc@domain.com, xyz@domain.com) 时,它似乎有效。当前代码如下,有问题的变量是recipientsTo

function FormEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetform = ss.getSheetByName("Sheet1"); // Name of the sheet which contains the results
  var lastrow = sheetform.getLastRow();
  var recipientsTO = sheetform.getRange(lastrow,3).getValue() + "@domain.com";
  var recipientsCC = ""
  var team = sheetform.getRange(lastrow,5).getValue();
  var datestamp = Utilities.formatDate(sheetform.getRange(lastrow,1).getValue(), "GMT - 5", "yyyy-MM-dd");
  var html = "Intro text;

  //The questions order in the email will be the order of the column in the sheet  
    for (var i = 2; i < 11; ++i) {
    html = html + "<b>" + sheetform.getRange(1,i).getValue() + "</b><br>";
    html = html + sheetform.getRange(lastrow,i).getValue() + "</p><br>";
       }

  //Add additional emails if entered in form field
  if (sheetform.getRange(lastrow,4).getValue() !== "") {
    recipientsTO = recipientsTO + "," + sheetform.getRange(lastrow,4).getValue()
  }

  //CC if response to a question is "Yes"
  if (sheetform.getRange(lastrow,10).getValue() == "Yes") {
    recipientsCC = "ccEmaIL@gmail.com"
  }


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


}
4

2 回答 2

3

根据 sendEmail(message) 文档,TO 字段只有一个收件人。而抄送字段可以有多个收件人,用逗号分隔。

http://goo.gl/CGjiJ

`to - 字符串 - 收件人的地址。

cc -String - 以逗号分隔的电子邮件地址列表,以抄送

另一种选择是在该函数中使用 sendEmail(String,String,String,Object) “recipient String 收件人的地址,用逗号分隔”。

希望这可以帮助。

于 2013-05-02T19:27:48.873 回答
1

这是我的生产脚本中的代码:

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

function getTab(name) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  return sheet.getSheetByName(name);
}

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

于 2020-10-02T10:56:32.173 回答