Gmail API with Attachments

666 Views Asked by At

How can I send an email message from Apps Script using the Gmail API that picks up a file from the Drive and includes in the message as an attachment.

I've looked in the official documentation but could not get this to work.

1

There are 1 best solutions below

0
On

You can accomplish this without using the GMail API, using the GmailApp Service instead. In fact, your use case is the example provided in the documentation for GmailApp.sendEmail().

 // Send an email with a file from Google Drive attached as a PDF.
 var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
 GmailApp.sendEmail('[email protected]', 'Attachment example', 'Please see the attached file.', {
     attachments: [file.getAs(MimeType.PDF)],
     name: 'Automatic Emailer Script'
 });