我可以使用脚本发送 HTML 格式的电子邮件吗?

Can I use script to send an email with HTML formatting?

我正在为 Google 表格创建发票脚本插件。我希望能够为 sheet 中包含电子邮件的单元格写入一个值。我现在了解如何通过 Google 脚本发送电子邮件,但是,我只能发送没有任何格式的简单消息。我的最终目标是能够发送包含图像、图表 [其中包含发票数据] 以及其他格式(例如段落和字体)的电子邮件。

我试图将 HTML 添加到实际的单元格中,但它只是发送带有 HTML 的电子邮件作为消息 [没有对电子邮件应用任何实际格式]。例如:我试图通过将以下内容放入 sheet 单元格 B2 来添加两个段落:

Hello World. \n My name is Jennifer. \n What is your name?  

结果与消息中写入的 \n 完全一样。

 function sendEmail2(){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3').getRange('B2');
var emailAddress = emailRange.getValues();


  //email info
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
var range = sheet.getRange('C2');
  var newMessage = range.getValue();
// Send Alert Email.
var message = 'This is your Alert email!'; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail(emailAddress, subject, newMessage);
}

电子邮件已发送,但电子邮件中没有实际的 HTML 格式。

我们如何通过 Google Apps 脚本发送带格式的电子邮件?

要发送包含图像、图表、格式等的电子邮件,您可能需要查看 how to create and server HTML in Google Apps Script as well as the section on Templated HTML

对于换行符等更简单的格式,有两种选择:

  1. 您可以在 Mac 上使用 ⌘ + Enter 或在 Windows 上使用 Ctrl + Enter 在单元格中添加换行符(而不是在单元格文本中使用“\n”)。当您获取此单元格的值并将其作为电子邮件发送时,电子邮件将包含换行符。
  2. 或者将电子邮件的每一行写在不同的单元格中。然后像这样将邮件的消息串在一起:

   var message = cell1Value + "\n" + cell2Value;

尝试使用 htmlbody 附加正文并使用 <br /> 而不是 /n

我将 Hello World. <br /> My name is Jennifer.<br /> What is your name? 存储在 A1 单元格中:

var body = sheet.getRange("A1").getValue();

   MailApp.sendEmail({
    to: "email@example.com",
    subject: "Your Google Spreadsheet Alert",
    htmlBody:  '<html><body>' + body + '</body></html>'});

这是一个示例,说明如何使用 Apps 脚本发送 HTML 电子邮件并将图表附加到 HTML 文档。它基于我拥有的电子表格。您可以使用 HTML 主体创建一个变量,并用变量替换您想要的信息,然后从同一个电子表格中检索信息。

 function sendEmailHtml(){
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
  var as = activeSheet.getSheets()[0]; /*selecting the very first sheet within the spreadsheet*/ 
  var name = activeSheet.getRange("A2:A2").getValue();
  var age = activeSheet.getRange("B2:B2").getValue();  
  var country = activeSheet.getRange("C2:C2").getValue();  
  var fontColor,chartsTable;
  var email = "email@mail.com";/*send the email to*/
  var subject = "Information";    
  var charts = as.getCharts();  
  var chartBlobs=new Array(charts.length);   
  var emailImages={};
  for(var i=0;i<charts.length;i++){
    var builder = charts[i].modify();   
    var newchart = builder.build().getBlob();
    chartBlobs[i]= newchart.getAs('image/png');   
    chartsTable = "<table>" +
    "<tr>" +
      "<th><p>Chart</p><img width='400' height='250'src='cid:chart"+0+"'></th>" +  /*adding the chart 0 on the table*/    
     "</tr>"+     
      "</table>";    
     emailImages["chart"+i]= chartBlobs[i];    
    Logger.log(charts[i].getChartId());
  } 
  var body = "<html><head><style type='text/css'>.ritz .waffle a { color: inherit; }.ritz .waffle .s1{border-bottom:1px SOLID #000000;border-right:1px SOLID #000000;border-left:1px SOLID #000000;background-color:#ffffff;text-align:left;color:#000000;font-family:Arial;font-size:10pt;vertical-align:bottom;white-space:nowrap;direction:ltr;padding:2px 3px 2px 3px;}.ritz .waffle .s0{border-bottom:1px SOLID #000000;border-right:1px SOLID #000000;background-color:#1155cc;text-align:left;font-weight:bold;color:#ffffff;font-family:Arial;font-size:10pt;vertical-align:bottom;white-space:nowrap;direction:ltr;padding:2px 3px 2px 3px;}</style></head><body><div class='ritz' dir='ltr'><table class='waffle' cellspacing='0' cellpadding='0'><tbody><tr style='height:20px;'><th id='0R0' style='height: 20px;' class='row-headers-background'><div class='row-header-wrapper' style='line-height: 20px;'></div></th><td style='width:100px' class='s0' dir='ltr'>Name</td><td style='width:100px' class='s0' dir='ltr'>Age</td><td style='width:100px' class='s0' dir='ltr'>Country</td></tr><tr style='height:20px;'><th id='0R1' style='height: 20px;' class='row-headers-background'><div class='row-header-wrapper' style='line-height: 20px;'></div></th><td class='s1 '>" + name +  "</td><td class='s1'>"+age+"</td><td class='s1'>"+country+"</td></tr></tbody></table></div></body></html><br>" + chartsTable;  
  MailApp.sendEmail(email, subject, body, {htmlBody: body, name:'Department Name', inlineImages:emailImages}); /*the inlineImages adds the png to the email's body*/ 
}

电子表格如下所示