From 2321b4b272c568ebcebf993c051c8ab60f651748 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 19 Dec 2014 22:33:17 +0200 Subject: [PATCH 1/3] Change from header in email, fixes #765 --- conf/app.ini | 2 +- modules/mailer/mailer.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/conf/app.ini b/conf/app.ini index d6ee6ae4d..d11788467 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -98,7 +98,7 @@ SUBJECT = %(APP_NAME)s HOST = ; Do not verify the certificate of the server. Only use this for self-signed certificates SKIP_VERIFY = -; Mail from address +; Mail from address. This can be just an email address, or the "Name" format (including the quotes and brackets) FROM = ; Mailer user name and password USER = diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 211ad59cd..22f403ee5 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -35,8 +35,7 @@ func (m Message) Content() string { } // create mail content - content := "From: \"" + m.From + "\" <" + m.User + - ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body + content := "From: " + m.From +"\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body return content } From edbe1de026cbc6d138a4382a3207c9f4f8280193 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 19 Dec 2014 22:48:21 +0200 Subject: [PATCH 2/3] Remove unused "User" member of Message Struct and fix bounce address The User member of a message is not needed anymore. The from that is send to the server, should always be the "system" from. This is also called the Bounce address http://en.wikipedia.org/wiki/Bounce_address --- modules/mailer/mail.go | 4 +--- modules/mailer/mailer.go | 11 +++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go index 6c73e7e58..611e7d8db 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -30,9 +30,7 @@ const ( // Create New mail message use MailFrom and MailUser func NewMailMessageFrom(To []string, from, subject, body string) Message { - msg := NewHtmlMessage(To, from, subject, body) - msg.User = setting.MailService.User - return msg + return NewHtmlMessage(To, from, subject, body) } // Create New mail message use MailFrom and MailUser diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 22f403ee5..3ce14822f 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -20,7 +20,6 @@ type Message struct { From string Subject string Body string - User string Type string Massive bool Info string @@ -35,7 +34,7 @@ func (m Message) Content() string { } // create mail content - content := "From: " + m.From +"\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body + content := "From: " + m.From + "\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body return content } @@ -66,7 +65,7 @@ func processMailQueue() { } // sendMail allows mail with self-signed certificates. -func sendMail(settings *setting.Mailer, from string, recipients []string, msgContent []byte) error { +func sendMail(settings *setting.Mailer, recipients []string, msgContent []byte) error { host, port, err := net.SplitHostPort(settings.Host) if err != nil { return err @@ -125,7 +124,7 @@ func sendMail(settings *setting.Mailer, from string, recipients []string, msgCon } } - if err = client.Mail(from); err != nil { + if err = client.Mail(settings.From); err != nil { return err } @@ -168,7 +167,7 @@ func Send(msg *Message) (int, error) { num := 0 for _, to := range msg.To { body := []byte("To: " + to + "\r\n" + content) - err := sendMail(setting.MailService, msg.From, []string{to}, body) + err := sendMail(setting.MailService, []string{to}, body) if err != nil { return num, err } @@ -179,7 +178,7 @@ func Send(msg *Message) (int, error) { body := []byte("To: " + strings.Join(msg.To, ";") + "\r\n" + content) // send to multiple emails in one message - err := sendMail(setting.MailService, msg.From, msg.To, body) + err := sendMail(setting.MailService, msg.To, body) if err != nil { return 0, err } else { From c884ecfea18b001ceb9cdbe98c8618a345f460e0 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 19 Dec 2014 23:06:03 +0200 Subject: [PATCH 3/3] Parse the from string to extract the email address --- conf/app.ini | 2 +- modules/mailer/mailer.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/conf/app.ini b/conf/app.ini index d11788467..4c2d146bb 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -98,7 +98,7 @@ SUBJECT = %(APP_NAME)s HOST = ; Do not verify the certificate of the server. Only use this for self-signed certificates SKIP_VERIFY = -; Mail from address. This can be just an email address, or the "Name" format (including the quotes and brackets) +; Mail from address, RFC 5322. This can be just an email address, or the "Name" format FROM = ; Mailer user name and password USER = diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index 3ce14822f..fd10c1e5a 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -8,6 +8,7 @@ import ( "crypto/tls" "fmt" "net" + "net/mail" "net/smtp" "strings" @@ -124,8 +125,12 @@ func sendMail(settings *setting.Mailer, recipients []string, msgContent []byte) } } - if err = client.Mail(settings.From); err != nil { + if fromAddress, err := mail.ParseAddress(settings.From); err != nil { return err + } else { + if err = client.Mail(fromAddress.Address); err != nil { + return err + } } for _, rec := range recipients {