Member-only story
Send attachments using the Google App Script
You can send email attachments using Google Apps Script by utilizing the MailApp service. Here’s a basic example of how to send an email with an attachment:
function sendEmailWithAttachment() {
var recipient = "recipient@example.com";
var subject = "Email with Attachment";
var body = "Please see the attached file.";
var attachmentFile = DriveApp.getFileById("FILE_ID_HERE"); // Replace "FILE_ID_HERE" with the ID of your file
MailApp.sendEmail({
to: recipient,
subject: subject,
body: body,
attachments: [attachmentFile.getAs(MimeType.PDF)] // Change the MimeType accordingly
});
}
Make sure to replace "recipient@example.com"
with the email address of the recipient, and "FILE_ID_HERE"
with the ID of the file you want to attach.
Additionally, you need to grant appropriate permissions to the script for accessing files. To do this, go to the “Resources” menu in the Apps Script editor, select “Advanced Google services”, and enable the “Drive API” service.
This is a basic example. Depending on your use case, you may need to adjust the code (for example, if you want to attach multiple files or specify a different MIME type for the attachment).