As a developer, it’s essential that you automate your workflow, and Google Apps Script can help with that. For instance, you can use it to send attachments with your email.

The Gmail API is Google’s way of interacting with Gmail. And with Google Apps Script, you can interact with the Gmail API to send emails, including attachments. In this article, you’ll learn how to send attachments in an email using Google Apps Script.

Getting Started

To get started, you’ll need a Gmail account, a text editor for writing the code, and a Google Apps Script project.

  1. Create a new Google Apps Script project by opening the script editor. From Google Drive, click the “New” button, hover your cursor over “More,” and then click “Google Apps Script.”
  2. In the Apps Script editor, click on “File” > “New” > “Script File”.
  3. Name the new file “SendEmailwithAttachment.gs” and click on “OK.”
  4. Now, you can start writing the code.

Code Explanation

In the code example below, replace the email address values with the email address you want to send the message to and the email address you want to send the message from. Also, replace the email message and subject values with your message and subject.

function sendEmailWithAttachment() {

    var emailTo = "example@email.com"; // the email recipient
    var emailFrom = "me@email.com"; // your email address
    var emailSubject = "Sending an email with an attachment";
    var emailBody = "This is the body of the email.";

    // grab the attachment file from Google Drive
    var file = DriveApp.getFileById("FILE_ID_HERE");

    // convert the file to bytes
    var attachmentBytes = file.getBlob().getBytes();

    // create an email message and attach the file
    var message = {
        to: emailTo,
        subject: emailSubject,
        body: emailBody,
        attachments: [{
            fileName: file.getName(),
            content: attachmentBytes,
            mimeType: file.getMimeType()
        }]
    };

    // send the email
    MailApp.sendEmail({
        to: message.to,
        cc: "",
        bcc: "",
        replyTo: "",
        subject: message.subject,
        body: message.body,
        attachments: message.attachments
    });
}

As you can see in the code example, the first step is to specify the email recipient, email sender, email subject, and email body message. In this example, we’ll use “DriveApp.getFileById()” to get the file to attach from Google Drive.

Next, we use the “getBlob()” function from the file object to convert the file to bytes. Then, we create an object “message” with the email message details and attach the file.

Finally, we use the “MailApp.sendEmail()” function to send the email.

Now that we’ve explained the code, let’s zoom in on the critical parts and show you how to use them.

Grabbing the Attachment File from Google Drive

The first step is to get the file you want to attach from Google Drive. In Google Drive, click on the file you want to attach and then click on the “3 vertical dots” > “Get link.”

Copy the link and then replace the “FILE_ID_HERE” placeholder with the file ID from the link.

var file = DriveApp.getFileById("FILE_ID_HERE");

Converting the File to Bytes

The next step is to convert the file to bytes so that we can attach it to the email message. To do this, use the “getBlob()” function of the file object.

var attachmentBytes = file.getBlob().getBytes();

Email Message with Attachment

Now that we have the attachment file in bytes, we can create an object “message.” In this example, we’ll use the “to,” “subject,” “body,” and “attachments” fields.

var message = {
    to: emailTo,
    subject: emailSubject,
    body: emailBody,
    attachments: [{
        fileName: file.getName(),
        content: attachmentBytes,
        mimeType: file.getMimeType()
    }]
};

Sending the Email

To send an email in Google Apps Script, we use the “MailApp.sendEmail()” function. Pass the message object with the email details as a parameter to this function.

MailApp.sendEmail({
    to: message.to,
    cc: "",
    bcc: "",
    replyTo: "",
    subject: message.subject,
    body: message.body,
    attachments: message.attachments
});