Apps Script lets you attach files to emails using GmailApp.sendEmail() with the attachments option. You can attach files from Google Drive, convert Google Sheets or Docs to PDF on the fly, or create in-memory blobs.
Attaching a File from Google Drive
functionsendEmailWithDriveAttachment(){var file =DriveApp.getFileById("YOUR_FILE_ID");var recipient ="[email protected]";GmailApp.sendEmail( recipient,"Your Contract – Pro Plan","Please find your contract attached.",{attachments:[file.getAs(MimeType.PDF)]});Logger.log("Email with attachment sent to "+ recipient);}
Converting a Google Sheet to PDF and Attaching It
This is one of the most common patterns — exporting a sheet as a PDF and emailing it directly:
functionsendSheetAsPDF(){var ss =SpreadsheetApp.getActiveSpreadsheet();var sheetId = ss.getSheetByName("Sales Tracker").getSheetId();var url ="https://docs.google.com/spreadsheets/d/"+ ss.getId()+"/export?format=pdf&gid="+ sheetId
+"&portrait=true&fitw=true";var token =ScriptApp.getOAuthToken();var response =UrlFetchApp.fetch(url,{headers:{Authorization:"Bearer "+ token }});var pdfBlob = response.getBlob().setName("Sales Tracker – March 2024.pdf");GmailApp.sendEmail("[email protected]","Sales Tracker – March 2024","Please find the monthly sales tracker attached.",{attachments:[pdfBlob]});Logger.log("Sales Tracker PDF emailed to manager.");}
Sending Individual Customer Reports from Sheet Data
This example generates a plain-text summary for each closed customer and sends it as a .txt attachment: