为什么 Gmail API 将 HTML 邮件发送为纯文本?

huangapple go评论97阅读模式
英文:

Why the gmail API sends html emails as plain text?

问题

我正在尝试使用Gmail API发送HTML邮件,但由于某些原因,它随机将邮件发送为纯文本。似乎Google修改了我设置的内容类型头。有什么原因吗?邮件内容始终完全相同(我进行了测试)。API仍然是实验性的吗?
有时,当它工作时,它还会添加Content-Type: multipart/alternative;(尽管我从未设置过)。

编码过程如下所示。代码是Go语言的,但我认为它很容易理解,过程与语言无关。

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()
英文:

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()

答案1

得分: 2

嗯,你确定这不是你程序中的一个错误吗?你能打印出整个字符串并粘贴在这里吗?

我刚刚使用Gmail API发送了一封邮件,内容如下:

收件人:<已删除>
主题:测试HTML邮件 2015-01-14 09:45:40
内容类型:text/html

<html><body><b>你好</b>世界</body></html>

在收件人的Gmail中,它看起来和预期一样。实际上,它似乎被包装在一个multipart/alternative中,并添加了一个text/plain部分(我认为这是个好事):

<随机跟踪头部>
MIME-Version: 1.0
发件人:<已删除>
日期:Wed, 14 Jan 2015 09:46:41 -0800
消息ID:<已删除>
主题:测试HTML邮件 2015-01-14 09:45:40
收件人:<已删除>
内容类型:multipart/alternative; boundary=089e0141a9a2875c38050ca05201

--089e0141a9a2875c38050ca05201
内容类型:text/plain; charset=UTF-8

*你好*世界

--089e0141a9a2875c38050ca05201
内容类型:text/html; charset=UTF-8

<html><body><b>你好</b>世界</body></html>
--089e0141a9a2875c38050ca05201--

无论如何,它正在进行一些解析/清理操作,但确实允许发送text/html格式的邮件。

英文:

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:
<pre>
To: &lt;redacted&gt;
Subject: test html email 2015-01-14 09:45:40
Content-type: text/html

&lt;html&gt;&lt;body&gt;&lt;b&gt;hello&lt;/b&gt;world&lt;/body&gt;&lt;/html&gt;
</pre>

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):
<pre>
&lt;random trace headers&gt;
MIME-Version: 1.0
From: &lt;redacted&gt;
Date: Wed, 14 Jan 2015 09:46:41 -0800
Message-ID: <redacted>
Subject: test html email 2015-01-14 09:45:40
To: &lt;redacted&gt;
Content-Type: multipart/alternative; boundary=089e0141a9a2875c38050ca05201

--089e0141a9a2875c38050ca05201
Content-Type: text/plain; charset=UTF-8

helloworld

--089e0141a9a2875c38050ca05201
Content-Type: text/html; charset=UTF-8

&lt;html&gt;&lt;body&gt;&lt;b&gt;hello&lt;/b&gt;world&lt;/body&gt;&lt;/html&gt;
--089e0141a9a2875c38050ca05201--
</pre>

In any case, it's doing some parsing/sanitizing but does allow sending text/html email.

huangapple
  • 本文由 发表于 2015年1月12日 12:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/27895290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定