Apps 脚本 - 将附件添加到电子邮件确认
Apps Script - add attachment to emailconfirmation
使用此脚本,我根据工作表中的某些字段发布了两封不同的电子邮件(索引 1 或索引 2),但我需要从 google 驱动器添加一个 .docx 文件作为已发布电子邮件的附件
这是我目前拥有的脚本,它正在运行我只需要从 google 驱动器添加 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
如何使用 Apps 脚本通过电子邮件从云端硬盘发送附件。
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
的列表。
要从 Drive 文件中获取 blob 源,您需要获取此文件的 ID,一旦有了它,您就可以像这样获取 blob:
const file = DriveApp.getFileById("[FILE_ID]")
const blob = file.getBlob()
当您将它传递给 sendEmail
时,请记住将其包含在 array
个 blob 中,如下所示:
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]
});
参考资料
使用此脚本,我根据工作表中的某些字段发布了两封不同的电子邮件(索引 1 或索引 2),但我需要从 google 驱动器添加一个 .docx 文件作为已发布电子邮件的附件
这是我目前拥有的脚本,它正在运行我只需要从 google 驱动器添加 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
如何使用 Apps 脚本通过电子邮件从云端硬盘发送附件。
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
的列表。
要从 Drive 文件中获取 blob 源,您需要获取此文件的 ID,一旦有了它,您就可以像这样获取 blob:
const file = DriveApp.getFileById("[FILE_ID]")
const blob = file.getBlob()
当您将它传递给 sendEmail
时,请记住将其包含在 array
个 blob 中,如下所示:
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]
});