sendmail Google Apps 脚本附件多个文件

sendmail Google Apps Script attachment multiple files

我使用 Google Apps 脚本发送带有文档附件的电子邮件 (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'
      }
    )}

这个有用吗?

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'
   }
 )}

让我先声明一下,我没有添加用于创建包含文件的数组的循环。然而,这是我对@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',} );