使用Gmail Apps脚本以HTML格式和纯格式发送电子邮件 [英] Sending an email in HTML and plain with a Gmail Apps Script

查看:147
本文介绍了使用Gmail Apps脚本以HTML格式和纯格式发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 <hello@example.com>"});

我需要用纯文本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 <hello@example.com>"});

此外,如果将正文设置为空白,大多数现代电子邮件客户端仍将能够渲染图像.

Also, if you set the body to blank, most modern email clients would still be able to render the image.

这篇关于使用Gmail Apps脚本以HTML格式和纯格式发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆