donglu1973 2015-01-12 04:24 采纳率: 100%
浏览 69

为什么gmail API以纯文本格式发送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
", k, v)
    }

    msg += "
" + em.Message
    gmsg := gmail.Message{
        Raw: encodeWeb64String([]byte(msg)),
    }
    _, err = gmailService.Users.Messages.Send("me", &gmsg).Do()
  • 写回答

1条回答 默认 最新

  • douyinjiao9351 2015-01-14 17:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题