使用此脚本,我将根据工作表中的某些字段发布两封不同的电子邮件(索引1或索引2(,但我需要从谷歌驱动器添加一个.docx文件作为已发布电子邮件的附件
这是我到目前为止的脚本,它正在工作,我只需要从谷歌驱动器添加docx作为附件文件。。请帮助
//Pulling all data from spreadsheet variables
var SpreadsheetID = "sheet id";
var SheetName = "namesheet";
var now = new Date();
var ss = SpreadsheetApp.openById(SpreadsheetID)
var sheet = ss.getSheetByName(SheetName);
var range = sheet.getDataRange();
var data = range.getValues();
function project_Notification() {
Logger.log(sheet.getLastRow() + " Is the last Row.");
for (var row = 1; row < sheet.getLastRow(); row++) {
var Notification = (data[row][12])
var Email_sent = (data[row][13])
var Admin_email = (data[row][5])
if (Notification == "Yes" && Email_sent == "" && Admin_email != "") {
Logger.log(Notification)
SendEmailConfirmation(data[row][5], data[row][1], data[row][14], data[row][3], data[row][6], data[row][9], data[row][10], data[row][11])
var Notification_cell = sheet.getRange("N" + (row + 1));
Logger.log("N" + (row + 1))
var Today = Utilities.formatDate(new Date(), "GMT", "dd/MM/yyyy");
Notification_cell.setValue(now);
}
}
}
function SendEmailConfirmation(email, Company, Admin_Name, Manager_Email, Landing_Page, QL_code, QL_url, PS_code) {
if (email != "" && Landing_Page != "") {
Logger.log(email)
var admin = {
"name": Admin_Name,
"company": Company,
"landingPage": Landing_Page,
"QLcode": QL_code,
"QLurl": QL_url,
"PScode": PS_code
};
var html1 = HtmlService.createTemplateFromFile('Index1.html');
html1.name = admin.name;
html1.comp = admin.company;
html1.landingPage = admin.landingPage;
html1.qlcode = admin.QLcode;
html1.qlurl = admin.QLurl;
var html2 = HtmlService.createTemplateFromFile('Index2.html');
html2.name = admin.name;
html2.comp = admin.company;
html2.landingPage = admin.landingPage;
html2.qlcode = admin.QLcode;
html2.qlurl = admin.QLurl;
html2.pscode = admin.PScode;
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0 && Landing_Page == "Product1") {
GmailApp.sendEmail(email, "Get started with your trial on Product1", '', {
'from': "product1@gmail.com",
replyTo: "product1@product.com",
cc: Manager_Email,
htmlBody: html1.evaluate().getContent()
});
} else if (Landing_Page == "Product2") {
GmailApp.sendEmail(email, "Get started with your trial on Product2", '', {
'from': "product2@gmail.com",
replyTo: "product2@product.com",
cc: Manager_Email,
htmlBody: html2.evaluate().getContent()
});
}
else {
GmailApp.sendEmail("products-admins@gmail.com", 'Script error', 'Please check the Products overview sheet for errors.');
}
}
}
//Send email for a confirmation that the script run correctly
如何使用应用程序脚本通过电子邮件从驱动器发送附件
GmailApp.sendEmail()
有一个attachments
高级参数。参数为:
GmailApp.sendEmail(recipient, subject, plainTextBody, options);
使用您的示例,脚本的当前状态为:
GmailApp.sendEmail(email, "Get started with your trial on Product1", '', {
'from': "product1@gmail.com",
replyTo: "product1@product.com",
cc: Manager_Email,
htmlBody: html1.evaluate().getContent()
});
在options
中,您可以使用许多高级参数。您正在使用:
{
'from': "product1@gmail.com",
replyTo: "product1@product.com",
cc: Manager_Email,
htmlBody: html1.evaluate().getContent()
}
您可以将attachments
参数添加为BlobSource
的列表。
要从驱动器文件中获取blob源,您需要获得此文件的ID,一旦您拥有它,您就可以获得如下blob:
const file = DriveApp.getFileById("[FILE_ID]")
const blob = file.getBlob()
当您将其传递给sendEmail
时,只需记住将其包含在一个由Blob组成的array
中,如下所示:
const file = DriveApp.getFileById("[FILE_ID]")
const blob = file.getBlob()
GmailApp.sendEmail(email, "Get started with your trial on Product1", '', {
'from': "product1@gmail.com",
replyTo: "product1@product.com",
cc: Manager_Email,
htmlBody: html1.evaluate().getContent(),
attachments: [blob]
});
编辑
要做各种附件,您只需在列表中包括更多:
const file1 = DriveApp.getFileById("[FILE_ID1]")
const file2 = DriveApp.getFileById("[FILE_ID2]")
const blob1 = file1.getBlob()
const blob2 = file2.getBlob()
GmailApp.sendEmail(email, "Get started with your trial on Product1", '', {
'from': "product1@gmail.com",
replyTo: "product1@product.com",
cc: Manager_Email,
htmlBody: html1.evaluate().getContent(),
attachments: [blob1, blob2]
});
参考文献
- 发送电子邮件
BlobSource
- getFileById(id(
- getBlob((