Fixed the "from" and "to" inputs of the SendMail function

Closes #3.
This commit is contained in:
alexcesaro 2014-10-23 18:46:32 +02:00
parent 9d27291713
commit 4fcb155d12
2 changed files with 30 additions and 11 deletions

View File

@ -30,9 +30,9 @@ func TestMessage(t *testing.T) {
msg.SetBody("text/plain", "¡Hola, señor!")
want := message{
from: "=?UTF-8?Q?Se=C3=B1or_From?= <from@example.com>",
from: "from@example.com",
to: []string{
"=?UTF-8?Q?Se=C3=B1or_To?= <to@example.com>",
"to@example.com",
"tobis@example.com",
},
content: "From: =?UTF-8?Q?Se=C3=B1or_From?= <from@example.com>\r\n" +

View File

@ -97,7 +97,10 @@ func (m *Mailer) Send(msg *Message) error {
if err != nil {
return err
}
recipients, bcc := getRecipients(message)
recipients, bcc, err := getRecipients(message)
if err != nil {
return err
}
h := flattenHeader(message, "")
body, err := ioutil.ReadAll(message.Body)
@ -148,32 +151,48 @@ func getFrom(msg *mail.Message) (string, error) {
}
}
return from, nil
return parseAddress(from)
}
func getRecipients(msg *mail.Message) (recipients, bcc []string) {
func getRecipients(msg *mail.Message) (recipients, bcc []string, err error) {
for _, field := range []string{"Bcc", "To", "Cc"} {
if addresses, ok := msg.Header[field]; ok {
for _, addr := range addresses {
switch field {
case "Bcc":
bcc = addAdress(bcc, addr)
bcc, err = addAdress(bcc, addr)
default:
recipients = addAdress(recipients, addr)
recipients, err = addAdress(recipients, addr)
}
if err != nil {
return recipients, bcc, err
}
}
}
}
return recipients, bcc
return recipients, bcc, nil
}
func addAdress(list []string, addr string) []string {
func addAdress(list []string, addr string) ([]string, error) {
addr, err := parseAddress(addr)
if err != nil {
return list, err
}
for _, a := range list {
if addr == a {
return list
return list, nil
}
}
return append(list, addr)
return append(list, addr), nil
}
func parseAddress(field string) (string, error) {
a, err := mail.ParseAddress(field)
if a == nil {
return "", err
}
return a.Address, err
}