I would like to send bulk google mails with attachments using a spreadsheet. In this spreadsheet, i have put the email address, the content template, and the respective attachment urls.
While i try and execute the below code, everything went well except for the line item #21 "attachments.push(file.getAs(MimeType.PDF));", which shows up an error. While i remove the line#21 and execute, i can send the bulk emails with a HTML Attachment instead of a PDF which contains virus and it is marked as spam.
Can someone help me with the correct code to execute the same and send a pdf directly instead of HTML
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 3;
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];
var subject = "Attachments";
var message = row[4];
var options = {muteHttpExceptions:true};
var attachments = UrlFetchApp.fetch(row[3], options).getBlob();
attachments.push(file.getAs(MimeType.PDF));
var emailSent = row[0];
if (emailSent != EMAIL_SENT) {
GmailApp.sendEmail(emailAddress, subject, message, {attachments: attachments,});
sheet.getRange(startRow + i, 1).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}