我创建了一个Google脚本,通过电子邮件将多个图表作为图像发送。使用MailApp
时可以使用,并且图像位于电子邮件的正文中。使用GmailApp
时,图像作为附件而不在电子邮件正文中。我想使用GmailApp
是因为我可以使用别名,并且因为我想出了如何使电子邮件发送到人员列表的方法。 如何进行更改,以便GmailApp
函数发送包含正文中包含图表的电子邮件?这是sample spreadsheet的链接,下面是代码:
function SRpt2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sstotalsheets = ss.getNumSheets();
var clientCode = ss.getSheetByName("Info").getRange("C4").getDisplayValue();
var progmgr = ss.getSheetByName("Info").getRange("D11").getDisplayValue();
var sheetcount = 0;
var token = ScriptApp.getOAuthToken();
var sheets = ss.getSheets();
var blobs = [];
var subject = "Daily " + clientCode +" Digest";
var body = "Your daily update";
var recips = progmgr + ", neill@momentum-behavioral.com";
var emailImages={};
var emailBody="Charts<br>";
for (var i = 5; i < sheets.length ; i++ ) {
var sheet = sheets[i];
var charts = sheet.getCharts();
if(charts.length > 0) {
var template = HtmlService.createTemplateFromFile("reportTemplate");
template.date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM-dd-yyyy");
blobs[i] = charts[0].getBlob().getAs('image/png').setName("areaBlob");
emailBody= emailBody + "<img src='cid:chart"+i+"'><br>";
emailImages["chart"+i]= blobs[i];
}
}
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
if (aliases.length > 0) {
GmailApp.sendEmail(recips, subject, emailBody, {'from': aliases[0],inlineImages:emailImages});
}
MailApp.sendEmail({
to: recips,
subject: subject,
htmlBody: emailBody,
inlineImages:emailImages});
}
这是它所引用的html:
<html>
<h1>Daily Update <?= date ?></h1>
<p>
<img src="cid:chart" />
</p>
</html>
格式错误的电子邮件正文只是说出了实际的html源代码(如果不将其实际视为html,我不知道如何将其粘贴到stackoverflow中。)
答案 0 :(得分:1)
将HTML添加到options对象。并设置body参数和空字符串。
var options;
options = {};//Assign an empty object to the variable options
//{'from': aliases[0],inlineImages:emailImages}
options.from = aliases[0];
options.inlineImages = emailImages;
options.htmlBody = emailBody;
if (aliases.length > 0) {
GmailApp.sendEmail(recips, subject, "", options);//Leave body an empty string
}