我需要通过 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 输出。
确保电子邮件在所有电子邮件客户端中看起来与源文档一样是一个大主题 - 对于本论坛来说太多了。但是,请记住以下几点:
- 许多客户端(例如 GMail)需要内联样式,而不是单独的 css
style
block 。 (这就是您在 GMail 中看不到格式的原因。)请参阅 How do I use Google Apps Script to change a CSS page to one with inline styles? - 图像环绕信息附加到 Google 文档中的图像元素,但(我上次检查时)不适用于脚本。
关于html - 获取 Google 文档 HTML 内容,然后使用 GmailApp 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21245161/