我正在修改Google Apps脚本中的邮件合并项目。我面临的问题是如何使电子邮件正文显示内嵌图像。目前,在调用GmailApp.sendEmail()之后,原始电子邮件模板中的所有内嵌图像将显示为附件。将JavaScript数组转换为JSON对象
我的猜测是,内嵌图像,如果我找到一些方式来imgVars数组转换成这样的JSON对象(从example in GAS documentation拍摄)显示:
MailApp.sendEmail(
"[email protected]",
"Logos",
"",
{ htmlBody:
"inline Google Logo<img src='cid:googleLogo'> images! <br/> inline YouTube Logo <img src='cid:youTubeLogo'>",
inlineImages:
{ googleLogo: googleLogoBlob,
youTubeLogo: youtTubeLogoBlob
}
}
);
所以我尝试做的是转换一个这样的数组:
var array = { item1, item2, item3 };
要JSON对象是这样的:
var json = { item1Name: item1,
item2Name: item2,
item3Name: item3
};
下面是从邮件合并的代码片段我工作:
//---------------------------------------------------------------
// If there are inline images in the body of the email
// Find them and store them in an array, imgVars
//---------------------------------------------------------------
if(emailTemplate.search(/<\img/ != -1)) {
var inlineImages = {};
// Extract all images from the email body
var imgVars = emailTemplate.match(/<img[^>]+>/g);
// For each image, extract its respective title attribute
for (i in imgVars) {
var title = imgVars[i].match(/title="([^\"]+\")/);
if (title != null) {
title = title[1].substr(0, title[1].length-1);
for (j in attachments) {
if (attachments[j].getName() == title) {
inlineImages[title] = attachments[j].copyBlob();
attachments.splice(j,1);
}
}
var newImg = imgVars[i].replace(/src="[^\"]+\"/,"src=\"cid:"+title+"\"");
emailTemplate = emailTemplate.replace(imgVars[i],newImg);
}
}
}
objects = getRowsData(dataSheet, dataRange);
for (var i = 0; i < objects.length; ++i) {
var rowData = objects[i];
if(rowData.emailSent != "EMAIL_SENT") {
// Replace markers (for instance ${"First Name"}) with the
// corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = fillInTemplateFromObject(selectedTemplate.getSubject(), rowData);
GmailApp.sendEmail(rowData.emailAddress, emailSubject, emailText,
{name: e.parameter.name,
attachments: attachments,
htmlBody: emailText,
cc: cc,
bcc: bcc,
inlineImages: inlineImages});
您确定要创建** JSON **或只是一个JavaScript对象吗?你的第一个代码示例只显示一个对象。而实际上你的问题是什么?你没有问过任何问题。 – 2012-07-31 23:49:06
啊,到底是什么。让我们将对象文字重命名为JSON对象。 ':P' – 2012-08-01 01:49:52
噢,问题是如何我应该内联的图像阵列,以便它可以采取如在GmailApp.sendEmail()可选的参数的inlineImages参数格式。我不确定它是否应该是JSON或JavaScript对象,但我的猜测是它是一个JavaScript对象。 – KarBytes 2012-08-01 19:59:56