One of the power of Google Apps Script is its ability to send emails. This feature is useful in a variety of business workflows.

In this post, I will cover 3 things:

  1. How to send emails with plain text bodies
  2. How to send emails with HTML bodies and attachments
  3. How to display images in emails

How to send emails with plain text bodies

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:

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 is more advanced. It 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!");

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.

How to send emails with HTML bodies and attachments

To send an email with an HTML body using MailApp, 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,
});

You can use the attachmentsparameter to add attachments to your email. Here is how it is done with MailApp.

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. Here is the list of the available MIME types.

How to display images in emails.

We want to embed the image into the email itself, rather than linking to an image hosted elsewhere. This is so that when the image is not publicly accessible, or when the recipient is offline, the image can still be displayed.

1. 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
};

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

2. 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' />"; 
// refer to the image using its attribute given in the inlinImages object above. In this case, it's img_blob.

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

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

Shares:
Show Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *