I have the below code that works fine it's just a small addition I am hoping to achieve. I would like a pdf (maybe 2) to go as attachments with the email.
I am trying to do this the right way and appear to be getting it wrong or not answering comments correctly. I have reduced the script..
I have also changed the folder to file as per a comment. I have called it file2 as you can see another file exists within the script.
function Email11() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId;
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin");
var fileName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A19").getDisplayValue();
var folder = DriveApp.getFoldersByName("Sales Agreements").next();
var Blob = sheetName.getParent().getBlob().getAs('application/pdf');
folder.createFile(Blob).setName(fileName);
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
//var email = Session.getUser().getEmail();
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("F6");
var emailAddress = emailRange.getValue();
var stockNumber = ss.getRange("B7").getValue();
var styledStockNumber = "<span style='color:red; font-weight:bold;'>" + stockNumber + "</span>";
var subject = "Darrin " + stockNumber;
var htmlBody =
styledStockNumber + " (Your Reference)<br><br>" +
"Please click the below link (Open the form) and complete the declaration form.<br><br>" +
"You will need the reference above (in red) to compete the form (4 numbers a space and the post code), " + "you can copy and paste it as it needs to be an identical match.<br><br>" +
"<a href='https://fill.boloforms.com/signature/9fd2152e-e490-4351-a4a8-56ab8287595f?p=view'>Open the form</a><br><br>" +
"Please consider the environment before printing this e-mail.";
var requestData = { "method": "GET", "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
var shID = getSheetID("S/A - Darrin") //Get Sheet ID of sheet name "Master"
var url = "https://docs.google.com/spreadsheets/d/" + ssID + "/export?format=pdf&id=" + ssID + "&gid=" + shID;
var result = UrlFetchApp.fetch(url, requestData);
var contents = result.getContent();
var bcc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("F5").getDisplayValues().flat().join(",");
var filename = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A19").getDisplayValue();
var myFile2 = DriveApp.getFileById("1T8REu_XSwfCV-dZz5ftV0KV_zTDe67oB");
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: htmlBody,
attachments: [result.getBlob().setName(`${filename}.pdf`),myFile2],
bcc: bcc
});
The statement below is assigning a DriveApp.File object to myFile2
var myFile2 = DriveApp.getFileById("1T8REu_XSwfCV-dZz5ftV0KV_zTDe67oB");
However, the attachments
property of the MailApp.send
parameter should be an array of Blob objects --ref. https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(Object)
Replace the above statement with:
var myFile2 = DriveApp.getFileById("1T8REu_XSwfCV-dZz5ftV0KV_zTDe67oB").getBlob().getAs(MimeType.PDF);