通过Google工作表从其他电子邮件地址发送带有html正文的电子邮件

时间:2017-02-20 15:23:24

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

我在使用另一个别名的电子邮件地址从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]的消息,而不是从别名发送它工作正常。

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

1 个答案:

答案 0 :(得分: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.