有附件问题的电子邮件

Email with attachment issue

我是这个脚本的完全新手,所以我环顾四周,找到了一些满足我需求的脚本。我目前正在尝试自动发送每月通讯,但不断收到以下错误:

异常:参数无效:附件(第 16 行,文件 "macros")

代码如下:

函数 sendEmails() {

var startRow = 1;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
var file = DriveApp.getFilesByName("Current Newsletter.pdf");
var data = dataRange.getValues();

for (i in data) {
   var row = data[i];
   var emailAddress = row[0];  // First column
   var message = "Dear "+ row[1] + "," + '\n\n' + "Please find attached, the latest update from [redacted].";
   var subject = "IAA Monthly Update";
   MailApp.sendEmail(emailAddress, subject, message, {attachments: [file]});
  }
}

试试这个……

MailApp.sendEmail(emailAddress, subject, message, {attachments:[file.getAs(MimeType.PDF)] });

根据文档,您无法在使用 MailApp 时发送带附件的电子邮件。相反,您需要使用 GmailApp。

以下是使用 GmailApp 得出解决方案的示例:

 // Send an email with a file from Google Drive attached as a PDF.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');

GmailApp.sendEmail('mike@example.com', 'Attachment example', 'Please see the attached file.', {
    attachments: [file.getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
});