I am struggling to send the content of a Google Document as HTML via Gmail, while trying to maintain the original formatting within the message. The current output is not accurate at all.
- When using Gmail, all formatting such as image wrapping, bold font, and colors are missing
- With Mozilla Thunderbird or Microsoft Outlook, only the image wrapping is lost, but there is some extra top margin added
Is there a way to improve this or make it work correctly?
Below is the code I am currently using:
function doc2mailtest() {
var docId = "1glvAaYuYm25oZZVmHLaEySlPP-9fIqGwm17wAs1HpHc";
// link to document: https://docs.google.com/a/thexs.ca/document/d/1glvAaYuYm25oZZVmHLaEySlPP-9fIqGwm17wAs1HpHc/edit
var html = getDocAsHtml(docId); // OK but not accurate - ignore text format and image wrapping
GmailApp.sendEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f8f0d5e6faf8f0bbf6faf8">[email protected]</a>", "Just testing MMD", '', { htmlBody: html });
return;
}
function getDocAsHtml(docId) {
var scope = 'https://docs.google.com/feeds/';
var url = scope+'download/documents/Export?exportFormat=html&format=html&id=';
var auth = googleOAuth_('docs',scope);
return UrlFetchApp.fetch(url+docId,auth).getContentText();
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
Thank you, Fausto