如何在 golang net/smtp.sendmail 中定义发件人姓名?

How to define sender's name in golang net/smtp.sendmail?

我的问题很简短,我有一个 golang 应用程序,我正尝试使用 net/smtp.sendmail:

从中发送电子邮件

这是我要使用的命令

smtp.SendMail(server, auth, **from**, to, msg)

通常不会有人将发件人的电子邮件地址传递给 from。无论如何我也可以传递发件人的姓名吗?

因此收件人将收到来自以下地址的电子邮件:

Foo Bar <foo@bar.com>

而不是来自

的电子邮件
<foo@bar.com>

我查了官方文档和网上的几十个例子也没找到。

谢谢。

例如,您可以将其添加到 msg,扩展 official docs 中的示例,您可以使用:

msg := []byte("From: the name <sender@example.org>\r\n" +
        "To: recipient@example.net\r\n" +
        "Subject: discount Gophers!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")

注意:From: the name <sender@example.org>

The msg parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of msg should be CRLF terminated. The msg headers should usually include fields such as "From", "To", "Subject", and "Cc".