发送电子邮件通过SMTP并更改发件人的名称

huangapple go评论150阅读模式
英文:

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 &lt;sender@example.com&gt;:

...
From: Sandy Sender &lt;sender@example.com&gt;
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 (
    &quot;gopkg.in/gomail.v2&quot;
)

func main() {
    m := gomail.NewMessage()
    m.SetAddressHeader(&quot;From&quot;, &quot;sender@example.com&quot;, &quot;Sandy Sender&quot;)
    m.SetAddressHeader(&quot;To&quot;, &quot;recipient@example.com&quot;)
    m.SetHeader(&quot;Subject&quot;, &quot;Hello!&quot;)
    m.SetBody(&quot;text/plain&quot;, &quot;This is the body of the message.&quot;)

    d := gomail.NewPlainDialer(&quot;smtp.example.com&quot;, 587, &quot;user&quot;, &quot;123456&quot;)

    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}

答案2

得分: 1

你可以在邮件头中添加"From: EmailName<" + EmailAdderss + ">\r\n",以显示你想要的任何EmailName,并添加Email地址以避免重复邮件。

英文:

You can add &quot;From: EmailName&lt;&quot; + EmailAdderss + &quot;&gt; \r\n&quot; 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(&quot;Domain Sender &lt;sender@domain.com&gt;&quot;)

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进行呈现。

我写了一个示例:

go_smtp.go

英文:

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:

go_smtp.go

huangapple
  • 本文由 发表于 2014年12月2日 20:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/27248953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定