【问题标题】:Google Apps Script sendEmail: multiple recipients string?Google Apps脚本sendEmail:多个收件人字符串?
【发布时间】:2013-05-02 15:19:14
【问题描述】:

使用 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,
});


}

【问题讨论】:

  • 您可以尝试在添加其他电子邮件地址的 if 语句下方添加 Logger.log(recipientsTO),然后查看日志以查看是否可以看到任何格式错误的地址。

标签: google-apps-script


【解决方案1】:

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

http://goo.gl/CGjiJ

`to - String - 收件人的地址。

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

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

希望这会有所帮助。

【讨论】:

  • 谢谢。您可能是对的,但我对那个文档页面感到困惑:link。在 sendEmail(message) 下它说“to”是“收件人的地址”,在 sendEmail(to, replyTo, subject, body) 下它说“to”是“收件人的地址,用逗号分隔”。不确定该相信哪个。无论如何,当附加的单元格值是一个收件人(“to:”中总共 2 个)但如果它是两个(“to”中总共 3 个)时,它就可以工作,这让我相信这只是糟糕的语法......
  • “相信哪个” - 每个函数都接收单独的参数,两者都相信。您可以调试并显示一个、两个或三个收件人的“to”字段的值吗?如果它是错误的语法,那么调试它应该会显示它。尝试在逗号和第三封电子邮件之间不要有任何空格。
  • ::facepalm:: 我上面的原始代码运行良好,所以请随意使用。非常感谢 mitrube 的帮助。我不熟悉调试器,但试过了,一切正常,有或没有空格!我得到的错误报告是指(lastrow,3)中的格式错误的电子邮件地址。不过,我确实通过经验学到了一些东西。再次感谢。
【解决方案2】:

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

//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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多