Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send a Google Doc in an email using Apps Script
/**
* 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". See here for information on how to find your API key:
* http://kb.mailchimp.com/article/where-can-i-find-my-api-key/.
*/
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'];
}
@dralkyr

This comment has been minimized.

Copy link

@dralkyr dralkyr commented Aug 23, 2018

The Drive service is now depreciated and none of this works from line 17 down. Do you have an updated copy with current functions?

@erickoledadevrel

This comment has been minimized.

Copy link
Owner Author

@erickoledadevrel erickoledadevrel commented Aug 23, 2018

I don't believe it is deprecated, where did you hear that?

@fsperoni

This comment has been minimized.

Copy link

@fsperoni fsperoni commented Feb 4, 2020

Hello Eric. I was trying to use your script and I'm getting an error on line 49 that says:
"Exception: Request failed for https://us4.api.mailchimp.com returned code 500. Truncated server response: {"status":"error","code":104,"name":"Invalid_ApiKey","error":"API key can not be blank"} (use muteHttpExceptions option to examine full response)"
I have created an account in mailchimp using the same email address I'm signed in to Apps Scripts. The API has been generated and I replaced it in the script along with the quotation marks. I'm not sure what I could be missing, wondering if you could please assist me.
Thank you,
Fabio

@erickoledadevrel

This comment has been minimized.

Copy link
Owner Author

@erickoledadevrel erickoledadevrel commented Feb 5, 2020

It could be that you cached an empty value first, before entering the key correctly. You'll just need to debug it more.

@fsperoni

This comment has been minimized.

Copy link

@fsperoni fsperoni commented Feb 5, 2020

I see, thank you Eric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment