收件地址不会显示在由golang smtp客户端发送的电子邮件中。

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

Receipt address won't show up in the email sent by golang smtp client

问题

以下是通过本地postfix服务器发送电子邮件的代码片段:

from := r.FormValue("from")
to := strings.Split(r.FormValue("to"), ";")
body := r.FormValue("body")
mime := "MIME-version:1.0;\nContent-Type:text/html;charset=\"UTF-8\";\n\n"
subject := fmt.Sprintf("Subject: %s\n", r.FormValue("subject"))
msg := []byte(subject + mime + body)

err := smtp.SendMail("localhost:25", nil, from, to, msg)

电子邮件已成功发送/接收。然而,在接收到的电子邮件的"To"字段中缺少收件人地址。我还尝试在Exchange服务器上进行了测试,收件人地址也缺失了。以下是电子邮件源代码中显示的内容:

To: Undisclosed recipients:;

有什么建议可以修复这个问题吗?谢谢!

英文:

Here is the code snippet to send the email via a local postfix server:

from := r.FormValue("from")
to := strings.Split(r.FormValue("to"), ";")
body := r.FormValue("body")
mime := "MIME-version:1.0;\nContent-Type:text/html;charset=\"UTF-8\";\n\n"
subject := fmt.Sprintf("Subject: %s\n", r.FormValue("subject"))
msg := []byte(subject + mime + body)

err := smtp.SendMail("localhost:25", nil, from, to, msg)

The email was sent/received fine. However, it is missing the receipt address in the To field of received email. I also tried it on an exchange server. The receipt addresses are missing as well. Here is what it shows in the email source.

To: Undisclosed recipients:;

Any suggestions to fix it? thanks!

答案1

得分: 2

你正在设置邮件信封的值,但是除了 Subject: 之外,你没有在邮件本身中放置任何头部信息。你还应该使用 \r\n 作为邮件的换行符。

一个最简单的示例可能如下所示:

headers := make(map[string]string)

headers["Subject"] = "this is a test"
headers["From"] = "me@example.com"
headers["To"] = "you@example.com"

body := "hello,\nthis is a test"

var msg bytes.Buffer
for k, v := range headers {
	msg.WriteString(k + ": " + v + "\r\n")
}

msg.WriteString("\r\n")
msg.WriteString(body)

一些其他有用的标准库包括:

英文:

You're setting the values for the mail envelope, but you haven't put any headers in the email itself except for Subject:. You should also be using \r\n as a newline for email.

A minimal example might look like:

headers := make(map[string]string)

headers["Subject"] = "this is a test"
headers["From"] = "me@example.com"
headers["To"] = "you@example.com"

body := "hello,\nthis is a test"

var msg bytes.Buffer
for k, v := range headers {
	msg.WriteString(k + ": " + v + "\r\n")
}

msg.WriteString("\r\n")
msg.WriteString(body)

Some other helpful stdlib packages:

huangapple
  • 本文由 发表于 2015年7月15日 03:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/31415529.html
匿名

发表评论

匿名网友

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

确定