是否可以使用Google Apps脚本在Html Body中发送具有不同数量内联图像的电子邮件



下午好,我目前正在使用下面的代码,该代码允许我在发送的一系列电子邮件的html正文中添加1个内联图像或无内联图像,从GDrive中获取放置在同一列中特定电子表格单元格中的图像ID。脚本工作正常,但我想知道,对于要发送的电子邮件循环(电子表格的每一行(,是否可以获取可变数量的ID,并且在那些具有多个ID的单元格中,这些ID用逗号分隔?F.i。第一个电子邮件ID单元格:Id1,Id2第二个电子邮件ID单元格:Id3、Id4、Id5、Id6第三个电子邮件ID单元格:(空白(第四个电子邮件ID单元格:Id7等等…

谢谢。

这是我实际的工作代码:

function sendEmails(){
var ss = SpreadsheetApp.getActive().getSheetByName('SendMail')
var lr = ss.getLastRow();
var quotaLeft = MailApp.getRemainingDailyQuota();
//Logger.log(quotaLeft);
if((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + 
(lr-1) + " emails. Emails were not send.");
} else {
for (var i = 2;i<=lr;i++){
var currentEmail = ss.getRange(i, 1).getValue();
var currentSubject = ss.getRange(i, 2).getValue();
var templateText = ss.getRange(i, 3).getValue();
var currentname = ss.getRange(i, 4).getValue();
var reply = ss.getRange(i, 5).getValue();
var imageFile = ss.getRange(i, 6).getValue();
var image = ""
Logger.log("Cell: " + ss.getRange(i, 6).getA1Notation());
Logger.log("Image: "+ DriveApp.getFileById(ss.getRange(i, 6).getValue()));
if(imageFile){
try{
image = DriveApp.getFileById(imageFile).getBlob();
} catch (e) {
templateText += "<p>Image merge failed: " + e;         
}
}
var message = templateText.replace("{name}",currentname);
message += "<br/><br/><img src="cid:sampleImage">";
if(image){
MailApp.sendEmail({
to: currentEmail,
replyTo: reply,
subject: currentSubject,
htmlBody: message,
inlineImages: {sampleImage: image},    
});
} else {
MailApp.sendEmail({
to: currentEmail,
replyTo: reply,
subject: currentSubject,
htmlBody: message,  
});
}
} //close for loop
} //close else statement
} //close sendEmails

您可以获得ID行,并使用循环为内联图像构建HTML

function sendMail(){
var ss = SpreadsheetApp.getActive().getSheetByName('SendMail')
var lr = ss.getLastRow();
var lc = ss.getLastColumn();
var quotaLeft = MailApp.getRemainingDailyQuota();
if((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + 
(lr-1) + " emails. Emails were not send.");
} else {
for (var i = 2;i<=lr;i++){
var currentEmail = ss.getRange(i, 1).getValue();
var currentSubject = ss.getRange(i, 2).getValue();
var templateText = ss.getRange(i, 3).getValue();
var currentname = ss.getRange(i, 4).getValue();
var reply = ss.getRange(i, 5).getValue();
var imageID = ss.getRange(i, 6, 1, lc).getValues().toString().split(',').filter(String);
var image = {};
var message = templateText.replace("{name}",currentname);

for (var x = 0; x < imageID.length; x++){
try{
image["inlineImage"+x] = DriveApp.getFileById(imageID[x]).getBlob();   
message += '<br/><img src="cid:' + "inlineImage"+x +'" />';
} catch (e) {
templateText += "<p>Image merge failed: " + e;         
}
}
if(image){
MailApp.sendEmail({
to: currentEmail,
replyTo: reply,
subject: currentSubject,
htmlBody: message,
inlineImages: image,    
});
} else {
MailApp.sendEmail({
to: currentEmail,
replyTo: reply,
subject: currentSubject,
htmlBody: message,  
});
}
} //close for loop
} //close else statement
} //close sendEmails```

我对MailApp.sendEmail(收件人、主题、正文、选项(的阅读表明,你必须拥有这种html:

<img src="cid:imagekey1" /><img src="cid:imagekey2" />

对于这种命令:

MailApp.sendEmail({currentEmail,currentSubject,non Html body (required),(replyTo:reply,htmlBody:"....<img src="cid:imagekey1" /><img src="cid:imagekey2" />...." ,inlineImages: {imagekey1:imgblob1,imagekey2:imgblob2}})

这些是文档中唯一的高级选项:

attachments BlobSource[]    an array of files to send with the email (see example)
bcc String  a comma-separated list of email addresses to BCC
cc  String  a comma-separated list of email addresses to CC
htmlBody    String  if set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImages    Object  a JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />` 

注:它说身体论点是必需的。

name String the name of the sender of the email (default: the user's name) noReply Boolean true if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for G Suite accounts, not Gmail users replyTo String an email address to use as the default reply-to address (default: the user's email address)

我看不到任何to:选项。

因此,我相信你将不得不通过编程调整你的html与你必须发送的数字图像。

最新更新