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

标签 html css google-apps-script gmail google-docs

我需要通过 Gmail 以 HTML 形式发送 Google 文档内容,以完全按照文档中的形式显示邮件中的内容(请参阅下面的示例代码)

至少可以说,电子邮件收件人收到的 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a171f3a0915171f54191517" rel="noreferrer noopener nofollow">[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"};
}

谢谢和问候,福斯托

最佳答案

Google 文档有一个插件,可以发送文档的 HTML 电子邮件版本 - 打开“插件 > 获取插件”并搜索“email html”。

对于编码员的方法,Omar AL Zabir(用户 @oazabir)写了一篇很好的博客文章来介绍它:Google Docs to Clean HTML 。您无需依赖以 HTML 形式获取文档,而是遍历文档对象来收集每个元素并构建 HTML 输出。

确保电子邮件在所有电子邮件客户端中看起来与源文档一样是一个大主题 - 对于本论坛来说太多了。但是,请记住以下几点:

关于html - 获取 Google 文档 HTML 内容,然后使用 GmailApp 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245161/

相关文章:

php - 从上层目录导入 CSS 文件

javascript - HTML5 视频 Uncaught TypeError : . play is not a function

css - 有行跨度时如何定位第二行的单元格?

javascript - 使用 Google 表格创建嵌套 JSON 对象

javascript - Bootstrap 模式和 jQuery : form inputs are empty

html - 在 h2 标签中放置带填充的背景图像

android - 用于移动设备和最大宽度桌面窗口的 Css @media

使用变量的 jQuery CSS 悬停

javascript - 将 google 脚本对象传递给 html 模板

google-apps-script - Google Apps 脚本函数 getActiveSheet() 不返回事件工作表