英文:
Send e-mail over smtp and change the sender's name
问题
我正在使用golang通过smtp发送电子邮件,这个功能运行得很好。为了设置电子邮件的发件人,我使用了Client.Mail函数:
func (c *Client) Mail(from string) error
当收件人收到电子邮件时,他会将发件人显示为纯文本的电子邮件地址:sender@example.com
我希望发件人的显示方式为:Sandy Sender <sender@example.com>
。
这个有可能吗?我尝试将发件人设置为Sandy Sender <sender@example.com>
或者只是Sandy Sender
,但是它们都不起作用。我收到了错误消息501 5.1.7 Invalid address
。
英文:
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: sender@example.com
I want the sender to be displayed like: Sandy Sender <sender@example.com>
.
Is this possible? I tried setting the sender to Sandy Sender <sender@example.com>
or only Sandy Sender
but none of them work. I get the error 501 5.1.7 Invalid address
答案1
得分: 18
你需要将邮件的From
字段设置为Sandy Sender <sender@example.com>
:
...
From: Sandy Sender <sender@example.com>
To: recipient@example.com
Subject: Hello!
This is the body of the message.
并且在Client.Mail
中只使用地址(sender@example.com
)。
或者,你可以使用我的包Gomail:
package main
import (
"gopkg.in/gomail.v2"
)
func main() {
m := gomail.NewMessage()
m.SetAddressHeader("From", "sender@example.com", "Sandy Sender")
m.SetAddressHeader("To", "recipient@example.com")
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)
}
}
英文:
You need to set the From
field of your mail to Sandy Sender <sender@example.com>
:
...
From: Sandy Sender <sender@example.com>
To: recipient@example.com
Subject: Hello!
This is the body of the message.
And use the address only (sender@example.com
) 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", "sender@example.com", "Sandy Sender")
m.SetAddressHeader("To", "recipient@example.com")
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)
}
}
答案2
得分: 1
你可以在邮件头中添加"From: EmailName<" + EmailAdderss + ">\r\n"
,以显示你想要的任何EmailName,并添加Email地址以避免重复邮件。
英文:
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.
答案3
得分: 0
你可以检查一下像jpoehls/gophermail
这样的项目是否更好。
它有一个测试用例像这个:
m.SetFrom("Domain Sender <sender@domain.com>")
它在内部(main.go
)调用了SetMailAddress()
方法,该方法在mail.ParseAddress()
中调用了RFC 5322中应该遵循的规范。
英文:
You can check if a project like jpoehls/gophermail
works better.
It has a test case like this one:
m.SetFrom("Domain Sender <sender@domain.com>")
It internally (main.go
) calls in SetMailAddress()
the method mail.ParseAddress()
which is supposed to follow RFC 5322.
答案4
得分: 0
我认为你可以使用mail.Address并使用Address.String函数来格式化地址。
func (a *Address) String() string
String函数将地址格式化为有效的RFC 5322地址。如果地址的名称包含非ASCII字符,则名称将根据RFC 2047进行呈现。
我写了一个示例:
英文:
I think you can use mail.Address and use Address.String function formats the address
func (a *Address) String() string
> String formats the address as a valid RFC 5322 address. If the address's name contains non-ASCII characters the name will be rendered according to RFC 2047.
and I write example:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论