Gmail应用会发送包含多个附件的电子邮件

时间:2017-09-18 07:23:15

标签: google-apps-script

我想学习使用电子表格填写附件的批量Google邮件。 在spreadsheet中,我已经放置了电子邮件地址,内容模板和附件网址。

以下是我发现的用于我的目的的代码:

var EMAIL_SENT = "EMAIL_SENT";

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  
  var numRows = 5; 
  var blobs = [];

  var dataRange = sheet.getRange(startRow, 1, numRows, 5)

  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[2];  // 3rd column
    var subject = "Attachments";       
    var message = row[4];       // 5th column
    var attachments = row[3];     // Fourth column
    var emailSent = row[0];     // 1st column
    if (emailSent != EMAIL_SENT) {  

      GmailApp.sendEmail(emailAddress, subject, message, attachments);

      sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);

      SpreadsheetApp.flush();
    }
  }
}

当我尝试午餐时,电子邮件已发送但没有附件。 请帮帮我。

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

您没有说明要发送的附件。我看到你在电子表格中调用了一些东西而不是附件。

尝试使用类似的东西

 var file = DriveApp.getFilesByName('attacheddoc.pdf');
  if (file.hasNext()) {
    MailApp.sendEmail(emailAddress, subject, message, {
    attachments: [file.next().getAs(attacheddoc.PDF)],
    name: 'Attached Documents'
  }

所以你的代码应该是

    var EMAIL_SENT = "EMAIL_SENT";

    function sendEmails() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var startRow = 2;  
      var numRows = 5; 
      var blobs = [];

      var dataRange = sheet.getRange(startRow, 1, numRows, 5)

      var data = dataRange.getValues();
      for (var i = 0; i < data.length; ++i) {
        var row = data[i];
        var emailAddress = row[2];  // 3rd column
        var subject = "Attachments";       
        var message = row[4];       // 5th column
        var attachments = row[3];     // Fourth column
        var emailSent = row[0];     // 1st column
        if (emailSent != EMAIL_SENT) {  

         var file = DriveApp.getFilesByName(attachments.pdf');
  if (file.hasNext()) {
    MailApp.sendEmail(emailAddress, subject, message, {
    attachments: [file.next().getAs(attachments+'.PDF')],
    name: 'Attached Documents'
  }
        }
      }
    }

希望这适合你。需要一些修补。但是,如果没有看到工作表,我真的无法做到这一点。如果它的工作原理+1 ?? :)