duanrenchuo9244
2017-09-22 16:48
浏览 153
已采纳

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

My question is short, I have a golang application from which I am trying to send an email using net/smtp.sendmail:

This is the command which I am going to use

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

No usually people pass the sender's email address to from. Is there anyway I can pass a sender's name as well?

So the recipient will get an email from:

Foo Bar <foo@bar.com>

instead of an email from

<foo@bar.com>

I checked the official documentation and dozens of examples online but could not find anything.

Thank you.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyun6781 2017-09-22 17:16
    已采纳

    You could add it to the msg for example, extending the example from the official docs you could use:

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

    Notice the: 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".

    打赏 评论

相关推荐 更多相似问题