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
", k, v)
}
msg += "
" + 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.