sendmail Google Apps脚本附件包含多个文件

时间:2015-11-07 18:05:28

标签: google-apps-script sendmail

我使用Google Apps脚本发送带有doc附件的电子邮件(anexo.doc) 我需要在同一封电子邮件中发送2个附件(anexo.doc和anexo2.doc)

我是怎么做到的? 我的实际代码是:

    file = DriveApp.getFilesByName('anexo.doc');

    if (file.hasNext()) {
      MailApp.sendEmail(tomail, subject, msg, {
      attachments: [file.next().getAs('application/msword')],
    name: 'Automatic Emailer Script'
      }
    )}

2 个答案:

答案 0 :(得分:2)

这有用吗?

file = DriveApp.getFilesByName('anexo.doc');
file2 = DriveApp.getFilesByName('anexo2.doc');

if (file.hasNext() && file2.hasNext()) {
  MailApp.sendEmail(tomail, subject, msg, {
    attachments: [
      file.next().getAs('application/msword'),
      file2.next().getAs('application/msword')],
    name: 'Automatic Emailer Script'
   }
 )}

答案 1 :(得分:0)

让我作为序言说我没有添加用于创建包含文件的数组的循环。但是,这是我对@CaringDev编写的代码的迷恋。

您也可以将它们存储在数组中并以这种方式选择,而不是在附件部分中获取文件。

 //code for the loop//
 var files = [];
    try {
       var rangefile = ss.getRange("a2:a3").getValues();
        //Logger.log(rangefile); 
       let i = 0;
       rangefile.forEach((fileIt) => {
       let drivefile = DriveApp.getFilesByName(rangefile[i]).next();
         files.push(drivefile);
        
       i=i+1;
       
       });
       
     }
    
    catch (error) {
      Browser.msgBox('File not in google drive.');
      throw new Error( "File not in google drive.");
    }
     //code to send email
         MailApp.sendEmail(
                    email, 
                    subject, 
                    message,
                    attachments: [
                      files[0],
                      files[1],
                      files[2]
                                 ],
                    name: 'Burton Griffin',} );