Google Apps脚本 - 通过电子邮件发送pdf

时间:2013-08-13 15:22:56

标签: email google-apps-script gmail

我有一个发送电子邮件的脚本。我想进行设置,以便通过我的google驱动器中的pdf附件发送电子邮件。该文件的名称是pdfholder.pdf

以下是当前正在运行的代码(没有附件)和发送电子邮件

MailApp.sendEmail(userEmail, subject, message);

以下代码无效(带附件),而不是发送电子邮件

var file = DocsList.getFileById('pdfholder');
MailApp.sendEmail(userEmail, subject, message, {attachments:file});

有关如何使这项工作的任何想法?我是谷歌应用程序脚本的新手,所以我非常感谢这些简单/彻底的解释。谢谢!

2 个答案:

答案 0 :(得分:4)

可选参数附件所需的参数是一个数组(如documentation中清楚显示的那样)。 这是为了便于处理多个附加文件。在你的情况下,它将是一个单个元素的数组:[file] 所以你的最终代码(确实)是

MailApp.sendEmail(userEmail, subject, message, {attachments:[file]});

答案 1 :(得分:1)

官方文档也有一个完全符合这个目的的例子:

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
 var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
 var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
 MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
     name: 'Automatic Emailer Script',
     attachments: [file.getAs(MimeType.PDF), blob]
 });

请参阅: https://developers.google.com/apps-script/reference/mail/mail-app