问题描述
我正在使用Google Apps脚本( http://script.google.com ).每次我使用GmailThread
的 Reply
回复邮件:
I'm writing an auto-replying bot for gmail using Google Apps Script (http://script.google.com). Each time I use GmailThread
's Reply
to reply to message:
var htmlbody = "Hello<br>This is a <b>test</b>.<br>Bye.";
var body = "Hello,\nThis is a test.\nBye.";
thread.reply(body, {htmlBody: htmlbody, from: "Myself <[email protected]>"});
我需要用纯文本body
和HTML用htmlbody
编写消息.
I need to write the message both in plain text body
and HTML in htmlbody
.
是否有一种方法只能以HTML格式编写电子邮件(以避免两次编写每个电子邮件内容(纯文本和HTML格式!)),并让reply()
都自动发送电子邮件是HTML和纯文本版本?
Would there be a way to write an email only in HTML (to avoid writing every email content twice, plain and HTML!), and let reply()
automatically send the email both in HTML and plaintext version?
我尝试过
var body = htmlbody.replace(/<br>/g,'\n').replace(/<b>/g,'');
// we should also replace </b> by '', etc.
但是这有点骇人听闻.有更好的版本吗?
but this is a bit of a hack. Is there a better version?
推荐答案
Google脚本无法自动生成纯文本部分,但是您编写了一个基于正则表达式的简单替换,该替换从HTML中删除了纯文本的所有标记.
Google Scripts cannot generate the plain text portion automatically but you write a simple regex based replaces that removes all the tags from the HTML for plain text.
var body = htmlBody.replace(/<.+?>/g, "");
thread.reply(body, {htmlBody: htmlbody, from: "Myself <[email protected]>"});
此外,如果将正文设置为空白,大多数现代电子邮件客户端仍将能够渲染图像.
Also, if you set the body to blank, most modern email clients would still be able to render the image.
这篇关于使用Gmail Apps脚本以HTML格式和纯格式发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!