为什么Gmail应用程序以纯文本形式发送HTML电子邮件? [英] Why the gmail API sends html emails as plain text?
问题描述
我正尝试使用Gmail API发送一封html电子邮件,但由于某些原因,它会以普通/文本形式随机发送电子邮件。 Google似乎改变了我设置的内容类型标题。这有什么理由吗?电子邮件的内容一直都是一样的(就像我测试的那样)。 API是否仍然是实验性的?
有时它在工作时也会添加 Content-Type:multipart / alternative;
(尽管我从未设置它)。 $ b
编码过程如下所示。
header:= make(map [string])这个代码是Go,但我想它是自我解释的,而且这个过程是语言不可知的。字符串)
header [From] = em.From.String()
header [To] = em.To.String()
// header [Subject] = encodeRFC2047(em.Subject)
header [Subject] = em.Subject
header [MIME-Version] =1.0
header [Content-Type] = text / html; charset = \utf-8 \
// header [Content-Transfer-Encoding] =base64
header [Content-Transfer-Encoding ] =quoted-printable
var msg string
for k,v:= range header {
msg + = fmt.Sprintf(%s:%s \r\\\
,k,v)
}
msg + =\r\\\
+ em.Message
gmsg:= gmail.Message {
Raw :encodeWeb64String([] byte(msg)),
}
_,err = gmailService.Users.Messages.Send(me,& gmsg).Do()
嗯,你是sur e它不是程序中的错误?您可以打印出整个字符串并将其粘贴到此处吗?
我只是使用Gmail API发送电子邮件,如:
收件人:< redacted>
Subject:test html email 2015-01-14 09:45:40
Content-type:text / html
< html>< body>< b>你好< / b个世界< /体>< / HTML>
,并且它看起来与收件人在Gmail中的结局一致。嗯,实际上它看起来像是将它包裹在一个multipart / alternative中,并且还添加了一个文本/纯文本部分(IMO):
pre
<随机跟踪标题>
MIME-Version:1.0
来自:< redacted>
日期:2015年1月14日星期三09:46:41 -0800
消息ID:
主题:test html email 2015-01-14 09:45:40
至:< redacted>
Content-Type:multipart / alternative; boundary = 089e0141a9a2875c38050ca05201
--089e0141a9a2875c38050ca05201
Content-Type:text / plain; charset = UTF-8
* hello * world
--089e0141a9a2875c38050ca05201
Content-Type:text / html; charset = UTF-8
< html>< body>< b> hello< / b> world< / body>< / html>
--089e0141a9a2875c38050ca05201--
在任何情况下,它都会进行一些解析/清理,但不会允许发送文本/ HTML电子邮件。
I'm trying to send a html email using the gmail API but for some reasons it randomly sends the email as plain/text. It seems that Google alters the content type header I set. Is there any reason for that? The email content is exactly same all the time (as I test it). Is the API still experimental?
Sometimes when it works it also adds Content-Type: multipart/alternative;
(although I never set it).
The encoding process looks as below. The code is Go but I guess it self explanatory and the process is language agnostic.
header := make(map[string]string)
header["From"] = em.From.String()
header["To"] = em.To.String()
// header["Subject"] = encodeRFC2047(em.Subject)
header["Subject"] = em.Subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/html; charset=\"utf-8\""
// header["Content-Transfer-Encoding"] = "base64"
header["Content-Transfer-Encoding"] = "quoted-printable"
var msg string
for k, v := range header {
msg += fmt.Sprintf("%s: %s\r\n", k, v)
}
msg += "\r\n" + em.Message
gmsg := gmail.Message{
Raw: encodeWeb64String([]byte(msg)),
}
_, err = gmailService.Users.Messages.Send("me", &gmsg).Do()
Hmm, are you sure it's not a bug in your program? Can you print out the entire string and paste it here?
I just used the Gmail API to send an email like:
To: <redacted> Subject: test html email 2015-01-14 09:45:40 Content-type: text/html <html><body><b>hello</b>world</body></html>
and it looked as expected by the recipient's end in Gmail. Well, actually looks like it got wrapped it in a multipart/alternative and added a text/plain part as well (good thing IMO):
<random trace headers> MIME-Version: 1.0 From: <redacted> Date: Wed, 14 Jan 2015 09:46:41 -0800 Message-ID: Subject: test html email 2015-01-14 09:45:40 To: <redacted> Content-Type: multipart/alternative; boundary=089e0141a9a2875c38050ca05201 --089e0141a9a2875c38050ca05201 Content-Type: text/plain; charset=UTF-8 *hello*world --089e0141a9a2875c38050ca05201 Content-Type: text/html; charset=UTF-8 <html><body><b>hello</b>world</body></html> --089e0141a9a2875c38050ca05201--
In any case, it's doing some parsing/sanitizing but does allow sending text/html email.
这篇关于为什么Gmail应用程序以纯文本形式发送HTML电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!