Allow mail with self-signed certificates
For more information, refer to https://groups.google.com/forum/#!topic/golang-nuts/c9zEiH6ixyw
Showing
1 changed file
with
54 additions
and
7 deletions
| ... | @@ -71,6 +71,59 @@ func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth { | ... | @@ -71,6 +71,59 @@ func (s *SmtpWriter) GetSmtpAuth(host string) smtp.Auth { |
| 71 | ) | 71 | ) |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | func (s *SmtpWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAddress string, recipients []string, msgContent []byte) error { | ||
| 75 | client, err := smtp.Dial(hostAddressWithPort) | ||
| 76 | if err != nil { | ||
| 77 | return err | ||
| 78 | } | ||
| 79 | |||
| 80 | host, _, _ := net.SplitHostPort(hostAddressWithPort) | ||
| 81 | tlsConn := &tls.Config{ | ||
| 82 | InsecureSkipVerify: true, | ||
| 83 | ServerName: host, | ||
| 84 | } | ||
| 85 | if err = client.StartTLS(tlsConn); err != nil { | ||
| 86 | return err | ||
| 87 | } | ||
| 88 | |||
| 89 | if auth != nil { | ||
| 90 | if err = client.Auth(auth); err != nil { | ||
| 91 | return err | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | if err = client.Mail(fromAddress); err != nil { | ||
| 96 | return err | ||
| 97 | } | ||
| 98 | |||
| 99 | for _, rec := range recipients { | ||
| 100 | if err = client.Rcpt(rec); err != nil { | ||
| 101 | return err | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | w, err := client.Data() | ||
| 106 | if err != nil { | ||
| 107 | return err | ||
| 108 | } | ||
| 109 | _, err = w.Write([]byte(msgContent)) | ||
| 110 | if err != nil { | ||
| 111 | return err | ||
| 112 | } | ||
| 113 | |||
| 114 | err = w.Close() | ||
| 115 | if err != nil { | ||
| 116 | return err | ||
| 117 | } | ||
| 118 | |||
| 119 | err = client.Quit() | ||
| 120 | if err != nil { | ||
| 121 | return err | ||
| 122 | } | ||
| 123 | |||
| 124 | return nil | ||
| 125 | } | ||
| 126 | |||
| 74 | // write message in smtp writer. | 127 | // write message in smtp writer. |
| 75 | // it will send an email with subject and only this message. | 128 | // it will send an email with subject and only this message. |
| 76 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { | 129 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| ... | @@ -89,13 +142,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { | ... | @@ -89,13 +142,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| 89 | mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username + | 142 | mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username + |
| 90 | ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) | 143 | ">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg) |
| 91 | 144 | ||
| 92 | err := smtp.SendMail( | 145 | err := s.sendMail(s.Host, auth, s.Username, s.RecipientAddresses, mailmsg) |
| 93 | s.Host, | ||
| 94 | auth, | ||
| 95 | s.Username, | ||
| 96 | s.RecipientAddresses, | ||
| 97 | mailmsg, | ||
| 98 | ) | ||
| 99 | 146 | ||
| 100 | return err | 147 | return err |
| 101 | } | 148 | } | ... | ... |
-
Please register or sign in to post a comment