Send Google Document as HTML via Gmail

The Google Script extracts the content of a Google Documents, converts the styles as inline CSS using the MailChimp API and send the document via Gmail as HTML Mail.

Sends an email using the contents of a Google Document as the body.

function sendDocument(documentId, recipient, subject) {
  var html = convertToHtml(documentId);
  html = inlineCss(html);
  GmailApp.sendEmail(recipient, subject, null, {
    htmlBody: html,
  });
}

Converts a file to HTML.

The Advanced Drive service must be enabled to use this function.

function convertToHtml(fileId) {
  var file = Drive.Files.get(fileId);
  var htmlExportLink = file.exportLinks['text/html'];
  if (!htmlExportLink) {
    throw 'File cannot be converted to HTML.';
  }
  var oAuthToken = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(htmlExportLink, {
    headers: {
      Authorization: 'Bearer ' + oAuthToken,
    },
    muteHttpExceptions: true,
  });
  if (!response.getResponseCode() == 200) {
    throw 'Error converting to HTML: ' + response.getContentText();
  }
  return response.getContentText();
}

Inlines CSS within an HTML file using the MailChimp API.

To use the API you must register for an account and then copy your API key into the script property “mailchimp.apikey”.

function inlineCss(html) {
  var apikey = CacheService.getPublicCache().get('mailchimp.apikey');
  if (!apikey) {
    apikey = PropertiesService.getScriptProperties().getProperty('mailchimp.apikey');
    CacheService.getPublicCache().put('mailchimp.apikey', apikey);
  }
  var datacenter = apikey.split('-')[1];
  var url = Utilities.formatString('https://%s.api.mailchimp.com/2.0/helper/inline-css', datacenter);
  var response = UrlFetchApp.fetch(url, {
    method: 'post',
    payload: {
      apikey: apikey,
      html: html,
      strip_css: true,
    },
  });
  var output = JSON.parse(response.getContentText());
  if (!response.getResponseCode() == 200) {
    throw 'Error inlining CSS: ' + output['error'];
  }
  return output['html'];
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

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.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.