【问题标题】:Sending an email with an html body from another email address via google sheets通过谷歌表格从另一个电子邮件地址发送带有 html 正文的电子邮件
【发布时间】:2017-02-20 15:23:23
【问题描述】:

我在使用另一个别名电子邮件地址从 Google 工作表发送 html 正文消息时遇到问题。

我可以使用 mailApp 发送它,但是当我切换到 GmailApp 时,我似乎无法让它工作。

我使用的脚本如下:

function sendNotification(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var row = sheet.getActiveRange().getRow();
  var cellvalue = ss.getActiveCell().getValue().toString();
  var emailAdd = "email@yourdomain.com";
  if(event.range.getA1Notation().indexOf("G") > -1 && sheet.getRange("G" + row).getDisplayValue() > 999 && emailAdd.length > 1)
  {
     var rowVals = getActiveRowValues(sheet);
    var aliases = GmailApp.getAliases();
Logger.log(aliases);
     GmailApp.sendEmail(
       "email@yourdomain.com",
       "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date,
       {htmlBody: "There has been a new allocation request from " + rowVals.name + " in the " + rowVals.team + " team.<br \> <br \> " 
       + "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Issuing Depot</th><th>Delivery Date</th><th>Case Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>" 
       + "<br \>To view the full details of the request, use the link below.<br \> <br \>" + 
       "<a href=\"https://docs.google.com/spreadsheets\">Allocation Requests</a>"
       +"<br \> <br \><i>This is an automated email. Please do not reply to it.<\i>"},
       {from: aliases[0]}
                        );
  }
}

  function getActiveRowValues(sheet){
  var cellRow = sheet.getActiveRange().getRow();
  // get depot value
  var depotCell = sheet.getRange("E" + cellRow);
  var depot = depotCell.getDisplayValue();
  // get date value
  var dateCell = sheet.getRange("F" + cellRow);
  var date = dateCell.getDisplayValue();
  // get quantity value
  var quantCell = sheet.getRange("G" + cellRow);
  var quant = quantCell.getDisplayValue();
  // return an object with your values
  var nameCell = sheet.getRange("B" + cellRow);
  var name = nameCell.getDisplayValue();
  var teamCell = sheet.getRange("C" + cellRow);
  var team = teamCell.getDisplayValue();
  return {
    depot: depot,
    date: date,
    quantity: quant,
    name: name,
    team: team
  } }

我已经设法让电子邮件从我的别名发送,但它只是发送包含 [object] 的消息,而不是从别名发送它工作正常。

有人可以看看我做错了什么吗?我还没有在这里找到答案。谢谢。

【问题讨论】:

  • sendEmail 有 4 个参数(TO, SUBJECT, Email BODY, OPTIONS) 您在电子邮件正文的第三个参数中有选项对象。你可能犯了这个错误,因为你试图把所有东西放在一个地方。您应该为选项对象和 Html 创建一个变量,以便 sendEmail() 更具可读性。
  • 谢谢,注意到我的错误。但是如何将变量设置为 html 正文?

标签: email google-apps-script google-sheets-api


【解决方案1】:

创建一个对象,然后向该对象添加元素:

var bodyHTML,o,sendTO,subject;//Declare variables without assigning a value

o = {};//Create an empty object

bodyHTML = "There has been a new allocation request from " + rowVals.name;

o.htmlBody = bodyHTML;//Add the HTML to the object with a property name of htmlBody
o.from = aliases[0]; //Add the from option to the object

sendTO = "email@yourdomain.com";
subject = "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date;

GmailApp.sendEmail(sendTO,subject,"",o);//Leave the third parameter as an empty string because the htmlBody advanced parameter is set in the object.

【讨论】:

  • 对不起,有点愚蠢,似乎无法让它发挥作用。我对脚本很陌生,所以显然做错了什么。我在哪里将它添加到我现有的脚本中以使其工作?
  • 变量声明应该在最上面。我在sendMail 前面省略了GmailApp 我只是添加了一个sendTO 变量。您是否收到错误消息?在 VIEW, EXECUTION TRANSCRIPT 中查看代码是否完成,如果失败,则失败在哪一行。
  • 我没有收到错误消息,但查看执行记录却失败了……执行失败:TypeError:在对象中找不到函数 sendMail
  • 没有sendMail() 方法。我犯了一个错误。应该是sendEmail
  • 完美。非常感谢您的帮助,非常感谢。
猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多