在Google Apps脚本中发送带附件的邮件会引发异常

时间:2017-08-29 14:35:04

标签: google-apps-script

我正在尝试使用下面的代码在我的邮件中附加一个html文档(来自Google驱动器):

function myFunction() {
    var message = 'Hi';
    var fileId ='0B0azXoe_2qFTzNYa1p5eUd0c2s'; // My html document ID
    var file = DriveApp.getFileById(fileId);
    var subject = 'Saved Transaction Cleardown logs';
    var blob = Utilities.newBlob('mail', 'application/vnd.google-apps.document', 'stc.html');  // I am not sure about the above line, Hope that is where i am wrong
      MailApp.sendEmail('XXXXXXX@gmail.com', subject,
                    message,
                    { attachments: [file.getAs(MimeType.HTML), blob],
                       name: 'Automatic Emailer Script'
                    });
} 

收到例外情况: 很抱歉,发生了服务器错误。请稍等一下再试一次。

1 个答案:

答案 0 :(得分:1)

这是实现目标的更合适方式:

function myFunction() {
    var message = 'Hi';
    var fileId ='0ByLGhvnaCIQsZXcteeRWd0e344eFE'; // My html document ID
    var file = DriveApp.getFileById(fileId);
    var subject = 'Saved Transaction Cleardown logs';
      GmailApp.sendEmail('someone@gmail.com', subject,
                    message,
                    { attachments: [ file],
                       name: 'Automatic Emailer Script'
                    });
}

更改html文件的ID。 你甚至不需要获得blob。查看here