英文:
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)
一些其他有用的标准库包括:
net/textproto
用于处理 MIME 头部net/mail
用于处理地址(尽管该包实际上只用于解析电子邮件)- http://gopkg.in/gomail.v1 提供了一个更完整的解决方案(可能还有其他很多)
英文:
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:
net/textproto
for MIME header handlingnet/mail
for address handling (though the package is really only for parsing email)- http://gopkg.in/gomail.v1 for a more complete solution (there are probably many others)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论