1

I want to emails with attachment but I have an error

SyntaxError: Invalid or unexpected token (row 17, file "try.gs")

and I don't understand why?. The pdf named "i.pdf" is in my google drive

here is my code :

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process

// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
var dataRange = sheet.getRange(startRow, 1, numRows, 4)

// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName(‘i.pdf’);
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: ‘Simple mail’});

}
}

Can someone help me pls

Azuka
  • 19
  • 4

2 Answers2

2

The error is caused by the use of typographic / curly / single quotation characters = ‘’.

Replace them by straight single or double quotation characters '' o "".

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
1

You have two typos when you declare var file and in the MailApp.sendEmail() function . Try this instead:

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process

// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
var dataRange = sheet.getRange(startRow, 1, numRows, 4)

// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName("i.pdf");
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: "Simple mail"});

}
}
Marios
  • 26,333
  • 8
  • 32
  • 52