Sending Emails with Google Apps Script: A Comprehensive Guide

Learn how you can send emails with Google Script. This tutorials helps you send email with an html body, attachments and display images in your email.

Google Apps Script is a powerful tool for automating tasks within the Google ecosystem. One of its most useful features is the ability to send emails programmatically, which can be used in a variety of business process automation scenarios.

This guide will walk you through how to send emails using Google Apps Script, sending simple emails as well as emails with HTML bodies and attachments, and even displaying images in emails.

Sending emails with MailApp and GmailApp

Google Apps Script provides two services for sending emails: MailApp and GmailApp.

MailApp is a simpler service that only allows the sending of emails. Here’s an example of how to send a simple email using MailApp:

MailApp.sendEmail("recipient@example.com", "Subject", "Hello, world!");

GmailApp lets you send email, but also allows you to compose drafts, manage labels, mark messages and threads, and conduct a variety of other Gmail account management tasks. Here’s how to send an email with GmailApp:

GmailApp.sendEmail("recipient@example.com", "Subject", "Hello, world!");

The difference between the two services is that GmailApp provides additional Gmail-specific functionality, such as managing labels and drafts, while MailApp only provides the ability to send emails.

Sending simple emails and emails from an HTML body

Both MailApp and GmailApp can send emails with plain text bodies, as shown in the examples above. But they can also send emails with HTML bodies, which allows for more complex and visually appealing emails.

To send an email with an HTML body, you need to use the body parameter to specify the HTML code. Here’s an example:

var htmlBody = "<p>Hello, <b>world</b>!</p>";
MailApp.sendEmail({
  to: "recipient@example.com",
  subject: "Subject",
  body: "A version of your email for email clients that don't support HTML",
  htmlBody: htmlBody,
});

If your HTML code is contained in an HTML file, you can use the HtmlService to create the HTML body:

var htmlBody = HtmlService.createHtmlOutputFromFile('my_html_file.html').getContent();
MailApp.sendEmail({
  to: "recipient@example.com",
  subject: "Subject",
  body: "A version of your email for email clients that don't support HTML",
  htmlBody: htmlBody,
});

Sending emails with attachments

Both MailApp and GmailApp can also send emails with attachments. Here’s an example of how to send an email with an attachment from Google Drive:

var file = DriveApp.getFileById("fileId"); // replace with your file's ID
MailApp.sendEmail({
  to: "recipient@example.com",
  subject: "Subject",
  body: "See attached file.",
  attachments: [file.getAs(MimeType.PDF)]
});

In the above example, getAs(MimeType.PDF) is used to get the file as a PDF. You can replace MimeType.PDF with other MIME types as needed.

Displaying an image in the email

To display an image in an email using Google Apps Script, you can use the cid: scheme in the src attribute of the img tag. This method involves embedding the image into the email itself, rather than linking to an image hosted elsewhere. This is particularly useful in scenarios where the image is not publicly accessible, or you want the image to be displayed even when the recipient is offline.

Here’s how you can do it: First, get the image file from Google Drive and attach it to the email with a specific cid (Content-ID). In this case, we’ll use image_blob as the cid.

var imageBlob = DriveApp.getFileById('imageId').getBlob(); // replace with your image's ID
var inlineImages = {
  "image_blob": imageBlob, // 'cid' of the img tag
  // add other cid to use in the html code
};

Then, in the HTML body of the email, you can reference this image using cid:image_blob in the src attribute of the img tag. Finally, send the email with the inline image. The key of the inlineImages object image_blob should match the cid used in the img.

var body = "See attached image.";
var htmlBody = "<p>" + body + "</p><img src='cid:image_blob' />";

MailApp.sendEmail({
  to: "recipient@example.com",
  subject: "Subject",
  body: body,
  htmlBody: htmlBody,
  inlineImages: inlineImages
});

That’s it! With these techniques, you can send a wide variety of emails using Google Apps Script. Happy scripting!


Joseph Asinyo

Google Workspace Developer

I’m Joseph. I love building applications and writing Google scripts to automate Google Workspace for greater productivity. Find more about me.

Scroll to Top