【问题标题】:Convert MailApp.sendEmail code to GmailApp.sendEmail with HTML Body将 MailApp.sendEmail 代码转换为带有 HTML 正文的 GmailApp.sendEmail
【发布时间】:2020-08-05 23:21:15
【问题描述】:

所有,我正在寻求有更多经验的人的帮助。我有一个拼凑而成的电子邮件脚本,效果很好。我想使用别名指定 from: 地址,据我了解,我需要使用 GmailApp 与 MailApp 来完成此操作。问题是,我不知道如何让我的 htmlBody 在 GmailApp 版本中工作。

这是我的工作 MailApp 代码:

function SendPreReleaseAlertEmail(row) { 
  var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
  var subject = sheet.getRange("M3").getValues();  // Change the subject as needed.
  var recipients = sheet.getRange("M2").getValue();  // Change the recipient as needed.
  var message = "<HTML><BODY>" + "<P>"
   for (var x=6;x<14;x++) {                                       //Loop from * to * with a +1 increment
     message = message + sheet.getRange("M" + x).getValues() + "<BR>"      //Add row I(x) to the message 
   }
    message = message + "</HTML></BODY>"; 
   MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}

这是我将其转换为带有 From 地址的 GmailApp 的失败尝试:

function SendPreReleaseAlertEmail(row) { 
  var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
  var subject = sheet.getRange("M3").getValues();  // Change the subject as needed.
  var recipients = sheet.getRange("M2").getValue();  // Change the recipient as needed.
  var message = "<HTML><BODY>" + "<P>"
   for (var x=6;x<14;x++) {                                       //Loop from * to * with a +1 increment
     message = message + sheet.getRange("M" + x).getValues() + "<BR>"      //Add row I(x) to the message 
   }
    message = message + "</HTML></BODY>"; 
   GmailApp.sendEmail(recipients, subject, {htmlBody: message}, {from: "moo@cow.com"});
}

上面的代码实际上是从别名发送电子邮件的。但是 htmlBody 在邮件正文中只有“[object Object]”字样。

它还活着!这是解决我的问题的最终代码:

GmailApp.sendEmail(recipients, subject, '', {htmlBody: message, from: "moo@cow.com"});

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    我认为你需要解决两件事。

    1.语法是

    sendEmail(recipient, subject, body, options) 
    

    因此,您可能需要在代码中为 body 添加一个空白占位符。

    2.您可能还需要在 options JavaScript 对象中同时包含 htmlBodyfrom,如下所示:

      GmailApp.sendEmail(recipients, subject, '' , {
        htmlBody: message, 
        from: "moo@cow.com"});
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    您仍然可以这样使用 MailApp:

    var email = "exampleRec@email.com";
    var Subject_to_Send = "This is an automated email";
      
    var check_body = 
         "Good morning team,  <br/><br/>" 
         +"I hope this email finds you well. <br/><br/>";
    
    MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, from: "exampleSen@email.com"}); 
    

    如果您还想发送 noReply 消息,您可以按如下方式调整最后一行:

    MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, noReply: true});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2018-07-03
      • 1970-01-01
      • 2021-07-10
      • 2017-01-01
      相关资源
      最近更新 更多