【问题标题】:Email with attachment issue带有附件问题的电子邮件
【发布时间】:2020-05-12 12:12:49
【问题描述】:

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

例外:无效参数:附件(第 16 行,文件“宏”)

代码如下:

函数 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]});
  }
}

【问题讨论】:

  • 错误在第 16 行,但没有第 16 行 ...更新您的代码

标签: email google-apps-script google-drive-api email-attachments


【解决方案1】:

根据文档,您在使用 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'
});

【讨论】:

  • 很高兴它有效。请在答案上打勾,以便此问题得到解决并对其他人可见。
【解决方案2】:

试试这个……

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

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 2019-12-24
    • 2011-12-05
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多