Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send e-mail over smtp and change the sender's name

I am sending e-mails over smtp in golang, which works perfectly fine. To set the sender of an e-mail I use the Client.Mail funtion:

func (c *Client) Mail(from string) error

When the recipient gets the e-mail he sees the sender as plaintext e-mail address: [email protected]

I want the sender to be displayed like: Sandy Sender <[email protected]>.

Is this possible? I tried setting the sender to Sandy Sender <[email protected]> or only Sandy Sender but none of them work. I get the error 501 5.1.7 Invalid address

like image 476
Kiril Avatar asked Dec 02 '14 12:12

Kiril


People also ask

Can you change the display name of the sender of incoming email?

When you send an email, the display name that appears next to your email address is called the Sender info. In Front, Sender info is tied to the signature that you use when sending an email. It can be changed in your signature settings at any time.

How do I change the name displayed when sending an email Outlook?

In Outlook, choose File > Account Settings > Account Settings. Select the email account that you want to change, and then choose Change. You can change your name on the Account Settings screen. To change the name that displays when you send email, update the Your name field.


Video Answer


2 Answers

You need to set the From field of your mail to Sandy Sender <[email protected]>:

...
From: Sandy Sender <[email protected]>
To: [email protected]
Subject: Hello!

This is the body of the message.

And use the address only ([email protected]) in Client.Mail.

Alternatively, you can use my package Gomail:

package main

import (
    "gopkg.in/gomail.v2"
)

func main() {
    m := gomail.NewMessage()
    m.SetAddressHeader("From", "[email protected]", "Sandy Sender")
    m.SetAddressHeader("To", "[email protected]")
    m.SetHeader("Subject", "Hello!")
    m.SetBody("text/plain", "This is the body of the message.")

    d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}
like image 185
Ale Avatar answered Oct 09 '22 09:10

Ale


You can add "From: EmailName<" + EmailAdderss + "> \r\n" to mail header to show whatever EmailName you want, and add Email Address to void duplicate mail.

like image 27
Binh Nguyen Avatar answered Oct 09 '22 11:10

Binh Nguyen