【问题标题】:Golang: How to UTF8 both subject header and body in email?Golang:如何在电子邮件中对主题标题和正文进行 UTF8 编码?
【发布时间】:2021-08-14 20:08:31
【问题描述】:

我对如何将包含主题标题和电子邮件正文的字符串(成功 - UTF8)传递给此函数很感兴趣:

func sendEmail(body string) {
    c, err := smtp.Dial(".....")

    if err != nil {
        log.Fatal(err)
    }

    defer c.Close()
    // Set the sender and recipient.
    c.Mail(".....")
    c.Rcpt(".....")

    // Send the email body.
    wc, err := c.Data()
    if err != nil {
        log.Fatal(err)
    }

    defer wc.Close()

    buf := bytes.NewBufferString(body)
    if _, err = buf.WriteTo(wc); err != nil {
        log.Fatal(err)
    }

}

然后我得到了主题标题和电子邮件正文;

body := "Subject: Header string which contains ŽČĆŠĐ in name of user " + name + "!\n" 
body += "Content-Type: text/html; charset=\"UTF-8\"\r\nContent-Transfer-Encoding: base64\r\n" 
body += "String inside email body which also might contain ŽČĆŠĐ" + year_month_day_hour_minute + " - " + end_of_shift

//function call
sendEmail(body)

我认为它应该可以正常运行......它确实正确显示了主题标题字符串(在 utf8 中),但由于某些未知原因,电子邮件正文的其余部分以垃圾形式显示。

我试图改变一些小细节,但原则上并没有真正改变。

这是我的第一个 Go 示例,因此我很容易忽略明显的问题。

感谢您对此事的任何想法!

【问题讨论】:

  • 您使用body 作为标题。这是错误的。标头有自己的约定来引用非 ASCII 字符,因此您可以对此类内容进行编码,或者查看您的库,了解如何添加主题和通用标头。
  • Data() 可用于标题和正文。 https://golang.org/pkg/net/smtp/ 并查看“func (*Client) Data”
  • 是的,然后我看到了包裹。不幸的是,该软件包是关于 SMTP 的,而不是电子邮件(两种不同的标准,但同时发布,一个紧挨着另一个)。我没有看到任何关于电子邮件的包(所以标题的编码,正确格式化标题,电子邮件标准日期格式,正文编码等),所以你可能想看看一个外部库。简而言之:标题应该是 ASCII + 一种转义:MIME“编码字”。最近邮件可以协商 UTF8 标头,扩展名:SMTPUTF8

标签: email go utf-8


【解决方案1】:

您好,我使用 golang 使用以下代码成功发送了 UTF8 电子邮件

func sendContactUs(name string, email string, userInput string) {
    // Sender data.
    from := "some@email.address"
    password := "some password"

    // Receiver email address.
    to := []string{
        "receipient@email.address",
    }

    // smtp server configuration.
    smtpHost := "smtp.gmail.com"
    smtpPort := "587"

    raw := `Subject: {name} Contact form on Web
Content-Type: text/plain; charset="UTF-8"


    Dear Manager,

    We receive a a form submission from webpage
    name  : {name}    
    email : {email}
    message:

    {message}

    Kind Regards
    XXXX  Mailing service team.
`

    raw = strings.Replace(raw, "{name}", name, -1)
    raw = strings.Replace(raw, "{email}", email, -1)
    raw = strings.Replace(raw, "{message}", userInput, -1)

    // Message.
    message := []byte(raw)

    // Authentication.
    auth := smtp.PlainAuth("", from, password, smtpHost)

    // Sending email.
    err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("Email Sent Successfully!")
}

请注意带有 Content-type: 的行必须从头开始。 换句话说,它不应该有任何前置空格。

另外,它后面必须有一个空行。

这是一个有效的代码。请试一试。如果您遇到任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    尝试: 在您的文本主题中使用此功能

    func cSubject(subject string) string {
        //return "=?iso-8859-1?Q?" + subject + "?="
        return "=?utf-8?q?" + subject + "?="
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多