Send Gmail Drafts with Inline Images using Google Scripts

Published in: Gmail - Google Apps Script

The Gmail Scheduler takes your Gmail drafts and send them at the schedule date and time. If a draft email message contains inline images, the ones that that are not hosted on the Internet but have been dragged on to Gmail at the time of composing the message, it uses this routine to parse and include them in the outgoing message.

The getBody() method retrieves the HTML content of a Gmail message but the base64 encoded inline images inside a Gmail draft need to be retrieved using the getRawContent() method. An alternative approach is here.

function sendInlineImages() {

  var message    = GmailApp.getDraftMessages()[0];
  var bodyText   = message.getBody();
  var rawContent = message.getRawContent();

  // Retrieve the multi-part boundary
  var multipartBoundary = rawContent.match(/multipart\/related; boundary=([\S]*)\s/i);

  // Skip for plain text or HTML emails without inline images
  if (multipartBoundary) {

    // Split the raw content on the multipart boundary to retrieve the inline content.
    var contentParts = rawContent.split("--" + multipartBoundary[1]);

    // Array of base64 encoded inline images
    var inlineImages = {};

    for ( var i in contentParts )
      var contentPart = contentParts[i].trim();

      // If this content part represents base64 encoded inline content?
      if ( contentPart.match(/content-disposition: inline;/i)
          && contentPart.match(/content-transfer-encoding: base64/i) ) {

        // Extract the mime-type and name.
        var contentType = contentPart.match(/content-type: ([^;]+);\s*name="([^"]+)"/i);
        var mimeType = contentType[1];
        var name = contentType[2];

        // Extract the content-id
        var contentID = contentPart.match(/content-id: <([^>]+)>/i)[1];

        // Split the content part into its header and base64 encoded data.
        // The header and base64 encoded part should be separated by a blank line.
        var subParts = contentPart.split(/^[^\S]+$/m);
        // Regex says split on lines that don't start with a non-space character

        // The 2nd part is the base64 encoded data.
        var base64enc = subParts[1].trim();

        // Create an image blob for the inline content.
        var blob = Utilities.newBlob(Utilities.base64Decode(base64enc), mimeType, contentID);

        inlineImages[contentID] = blob;

        // Replace the image source in the body text with the reference to the inline content.
        var regExp = new RegExp( "src=\"[^\"]+realattid=" + contentID + "[^\"]+\"", "g" );
        bodyText = bodyText.replace(regExp, "src=\"cid:" + contentID + "\"" );



  var subject = message.getSubject();
  var attachments = message.getAttachments();
  var bcc = message.getBcc();
  var cc = message.getCc();

  GmailApp.sendEmail("", "Inline Image Test", "labnol",
       { attachments:attachments, htmlBody:bodyText, cc:cc, bcc:bcc, inlineImages:inlineImages});
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube