Update smtp.go (develop branch)
For mail servers that do not require Authentication we must pass NIL for the SendMail parameter 2 (the auth parameter). Otherwise it fails to send the mail.
Showing
1 changed file
with
14 additions
and
6 deletions
| ... | @@ -54,6 +54,18 @@ func (s *SmtpWriter) Init(jsonconfig string) error { | ... | @@ -54,6 +54,18 @@ func (s *SmtpWriter) Init(jsonconfig string) error { |
| 54 | return nil | 54 | return nil |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth { | ||
| 58 | if len(strings.Trim(s.Username, " ")) == 0 && len(strings.Trim(s.Password, " ")) == 0 { | ||
| 59 | return nil | ||
| 60 | } | ||
| 61 | return smtp.PlainAuth( | ||
| 62 | "", | ||
| 63 | s.Username, | ||
| 64 | s.Password, | ||
| 65 | host, | ||
| 66 | ) | ||
| 67 | } | ||
| 68 | |||
| 57 | // write message in smtp writer. | 69 | // write message in smtp writer. |
| 58 | // it will send an email with subject and only this message. | 70 | // it will send an email with subject and only this message. |
| 59 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { | 71 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| ... | @@ -64,12 +76,8 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { | ... | @@ -64,12 +76,8 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| 64 | hp := strings.Split(s.Host, ":") | 76 | hp := strings.Split(s.Host, ":") |
| 65 | 77 | ||
| 66 | // Set up authentication information. | 78 | // Set up authentication information. |
| 67 | auth := smtp.PlainAuth( | 79 | auth := s.GetSmtpAuth(hp[0]) |
| 68 | "", | 80 | |
| 69 | s.Username, | ||
| 70 | s.Password, | ||
| 71 | hp[0], | ||
| 72 | ) | ||
| 73 | // Connect to the server, authenticate, set the sender and recipient, | 81 | // Connect to the server, authenticate, set the sender and recipient, |
| 74 | // and send the email all in one step. | 82 | // and send the email all in one step. |
| 75 | content_type := "Content-Type: text/plain" + "; charset=UTF-8" | 83 | content_type := "Content-Type: text/plain" + "; charset=UTF-8" | ... | ... |
-
Please register or sign in to post a comment