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

Viewed 1716

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 Answers

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>\r\n" +
        "To: recipient@example.net\r\n" +
        "Subject: discount Gophers!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")

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".

Related