【问题标题】:Apps Script - add attachment to emailconfirmationApps 脚本 - 将附件添加到电子邮件确认
【发布时间】:2021-05-17 11:15:04
【问题描述】:

使用此脚本,我将根据工作表中的某些字段发布两封不同的电子邮件(索引 1 或索引 2),但我需要从 google 驱动器添加一个 .docx 文件作为已发布电子邮件的附件

这是我到目前为止的脚本,它正在运行我只需要将 docx 添加为来自谷歌驱动器的附件文件。请帮助p

//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

【问题讨论】:

  • 您似乎不小心删除了帖子中的大部分信息,但在剩余的部分中仍然使用“此脚本”来引用它。我为你解开了那个。一般情况下,请不要在得到答案后编辑问题。问题应保持与答案之前一样完整和可回答。包括您最初提供的有用信息。

标签: email google-apps-script attachment email-attachments


【解决方案1】:

如何使用 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 时,请记住将其包含在 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]
       });

参考文献

【讨论】:

  • 那行得通..谢谢!跟进问题。如果我想添加多个文件怎么办?
  • 我编辑了我的答案以用两个文件进行演示
猜你喜欢
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 2018-11-27
  • 2018-01-04
  • 2016-02-26
相关资源
最近更新 更多