获取Google文档HTML内容,然后使用GmailApp发送



我需要将Google文档内容作为gmail作为html发送,在文档中的外观中完全显示了消息中的内容(请参见下面的示例代码)

电子邮件收件人收到的HTML内容至少不准确

  • 如果使用Gmail读取消息,则所有格式都缺少(图像包装,字体粗体和颜色)
  • 如果使用Mozilla Thunderbird或Microsoft Outlook仅缺少图像包装,但是有一些额外的最高边距

有什么方法可以做到这一点或至少更好?

在这里我使用的代码

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("me@some.com", "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"};
}

谢谢,问候,福斯托

Google文档有一个附加组件,可以发送文档的HTML电子邮件版本 - 打开"附加组件>获取附加组件",然后搜索"电子邮件html"。<<<<<<<<<<<</p>

对于编码员的方法,Omar al Zabir(用户@oazabir)写了一个很好的博客条目,可以浏览它:Google文档以清洁HTML。而不是依靠将文档作为HTML获取,而是穿越文档对象来收集每个元素并构建HTML输出。

确保电子邮件看起来就像所有电子邮件客户端中的源文档一样 - 对于这个论坛来说太多了。但是,以下是要牢记的一些要点:

  • 许多客户端(例如Gmail)需要在线样式,而不是单独的CSS style块。(这就是为什么您在Gmail中看不到格式的原因。)请参阅如何使用Google Apps脚本将CSS页面更改为具有内联样式的CSS页面?
  • 图像包装信息附加到Google文档中的图像元素,但(上次我检查)脚本不可用。

最新更新