Andrew Hobson
Andrew Hobson

Reputation: 13

Email with attachment issue

I am a complete novice with this script, so I looked around and found some that meets my needs. I am currently trying to automate the sending of a monthly newsletter and just keep getting the following error:

Exception: Invalid argument: attachments (line 16, file "macros")

The code is as follows:

function sendEmails() {

var startRow = 1;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
var file = DriveApp.getFilesByName("Current Newsletter.pdf");
var data = dataRange.getValues();

for (i in data) {
   var row = data[i];
   var emailAddress = row[0];  // First column
   var message = "Dear "+ row[1] + "," + '\n\n' + "Please find attached, the latest update from [redacted].";
   var subject = "IAA Monthly Update";
   MailApp.sendEmail(emailAddress, subject, message, {attachments: [file]});
  }
}

Upvotes: 0

Views: 676

Answers (2)

Neven Subotic
Neven Subotic

Reputation: 1429

According to the documentation you cannot send emails with attachments when using MailApp. Instead, you need to use the GmailApp.

Below is an example of arrive at a solution using the GmailApp:

 // Send an email with a file from Google Drive attached as a PDF.
var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');

GmailApp.sendEmail('[email protected]', 'Attachment example', 'Please see the attached file.', {
    attachments: [file.getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
});

Upvotes: 2

Manish Chaudhary
Manish Chaudhary

Reputation: 508

try this one...

MailApp.sendEmail(emailAddress, subject, message, {attachments:[file.getAs(MimeType.PDF)] });

Upvotes: 0

Related Questions