How to Send Emails with Multiple Attachments Using Google Apps Script

Email communication often requires sending multiple documents or files at once. Whether you’re sending weekly reports, project documents, or promotional materials, attaching multiple files to an email can significantly enhance your communication. Google Apps Script provides a straightforward way to automate sending emails with multiple attachments from Google Drive or any other accessible online storage. This post will guide you through setting up a script to send an email with more than one attachment using Google Apps Script’s Gmail service.

What You’ll Need

  • Google Drive with the files you want to attach or files stored locally that can be uploaded to Google Drive.
  • Basic knowledge of JavaScript and Google Apps Script.

Step 1: Prepare Your Files

Ensure that all files you wish to attach to your email are uploaded to Google Drive and are accessible by the script. Note their IDs as these will be required to attach them to the email.

Step 2: Open the Script Editor

  • Open Google Drive.
  • Click on “New” > “More” > “Google Apps Script” to create a new script.

Step 3: Write the Script to Send an Email

The script below demonstrates how to send an email with multiple attachments. It assumes you know the IDs of the files you want to attach.

function sendEmailWithAttachments() {
var emailAddress = 'recipient@example.com'; // The recipient's email address
var subject = 'Email with Multiple Attachments'; // The subject of the email
var messageBody = 'Please find attached the documents you requested.'; // The body of the email

// IDs of the files to attach
var fileIds = ['File-ID-1', 'File-ID-2', 'File-ID-3']; // Replace with actual file IDs from Google Drive

var attachments = fileIds.map(function(id) {
return DriveApp.getFileById(id).getBlob();
});

// Send the email
GmailApp.sendEmail(emailAddress, subject, messageBody, {
attachments: attachments
});
}

Step 4: Customize the Script

  • Replace 'recipient@example.com' with the actual recipient’s email address.
  • Replace 'File-ID-1', 'File-ID-2', 'File-ID-3' with the actual IDs of the files you want to attach.
  • Modify the subject and message body as needed.

Step 5: Run the Script

  • Save your script.
  • Run sendEmailWithAttachments by selecting the function in the dropdown menu and clicking the play button.
  • Authorize the script if prompted to allow it to send emails and access files on your behalf.

Step 6: Check Your Email

Verify that the email has been sent by checking your “Sent Mail” in Gmail or confirming with the recipient that they have received the email with all attachments.

Conclusion

Automating the process of sending emails with multiple attachments using Google Apps Script is an efficient way to handle bulk communications without manually attaching files every time. This approach is highly scalable and can be adapted to various needs, such as monthly invoicing, weekly reporting, or regular project updates.