1
1
package report
2
2
3
3
import (
4
+ "crypto/tls"
4
5
"fmt"
5
6
"net"
6
7
"net/mail"
@@ -87,6 +88,61 @@ type emailSender struct {
87
88
send func (string , smtp.Auth , string , []string , []byte ) error
88
89
}
89
90
91
+ func smtps (emailConf config.SMTPConf , message string ) (err error ) {
92
+ auth := smtp .PlainAuth ("" ,
93
+ emailConf .User ,
94
+ emailConf .Password ,
95
+ emailConf .SMTPAddr ,
96
+ )
97
+
98
+ //TLS Config
99
+ tlsConfig := & tls.Config {
100
+ ServerName : emailConf .SMTPAddr ,
101
+ }
102
+
103
+ smtpServer := net .JoinHostPort (emailConf .SMTPAddr , emailConf .SMTPPort )
104
+ //New TLS connection
105
+ con , err := tls .Dial ("tcp" , smtpServer , tlsConfig )
106
+ if err != nil {
107
+ return xerrors .Errorf ("Failed to create TLS connection: %w" , err )
108
+ }
109
+ defer con .Close ()
110
+
111
+ c , err := smtp .NewClient (con , emailConf .SMTPAddr )
112
+ if err != nil {
113
+ return xerrors .Errorf ("Failed to create new client: %w" , err )
114
+ }
115
+ if err = c .Auth (auth ); err != nil {
116
+ return xerrors .Errorf ("Failed to authenticate: %w" , err )
117
+ }
118
+ if err = c .Mail (emailConf .From ); err != nil {
119
+ return xerrors .Errorf ("Failed to send Mail command: %w" , err )
120
+ }
121
+ for _ , to := range emailConf .To {
122
+ if err = c .Rcpt (to ); err != nil {
123
+ return xerrors .Errorf ("Failed to send Rcpt command: %w" , err )
124
+ }
125
+ }
126
+
127
+ w , err := c .Data ()
128
+ if err != nil {
129
+ return xerrors .Errorf ("Failed to send Data command: %w" , err )
130
+ }
131
+ _ , err = w .Write ([]byte (message ))
132
+ if err != nil {
133
+ return xerrors .Errorf ("Failed to write EMail message: %w" , err )
134
+ }
135
+ err = w .Close ()
136
+ if err != nil {
137
+ return xerrors .Errorf ("Failed to close Writer: %w" , err )
138
+ }
139
+ err = c .Quit ()
140
+ if err != nil {
141
+ return xerrors .Errorf ("Failed to close connection: %w" , err )
142
+ }
143
+ return nil
144
+ }
145
+
90
146
func (e * emailSender ) Send (subject , body string ) (err error ) {
91
147
emailConf := e .conf
92
148
to := strings .Join (emailConf .To [:], ", " )
@@ -113,20 +169,28 @@ func (e *emailSender) Send(subject, body string) (err error) {
113
169
smtpServer := net .JoinHostPort (emailConf .SMTPAddr , emailConf .SMTPPort )
114
170
115
171
if emailConf .User != "" && emailConf .Password != "" {
116
- err = e .send (
117
- smtpServer ,
118
- smtp .PlainAuth (
119
- "" ,
120
- emailConf .User ,
121
- emailConf .Password ,
122
- emailConf .SMTPAddr ,
123
- ),
124
- emailConf .From ,
125
- mailAddresses ,
126
- []byte (message ),
127
- )
128
- if err != nil {
129
- return xerrors .Errorf ("Failed to send emails: %w" , err )
172
+ switch emailConf .SMTPPort {
173
+ case "465" :
174
+ err := smtps (emailConf , message )
175
+ if err != nil {
176
+ return xerrors .Errorf ("Failed to send emails: %w" , err )
177
+ }
178
+ default :
179
+ err = e .send (
180
+ smtpServer ,
181
+ smtp .PlainAuth (
182
+ "" ,
183
+ emailConf .User ,
184
+ emailConf .Password ,
185
+ emailConf .SMTPAddr ,
186
+ ),
187
+ emailConf .From ,
188
+ mailAddresses ,
189
+ []byte (message ),
190
+ )
191
+ if err != nil {
192
+ return xerrors .Errorf ("Failed to send emails: %w" , err )
193
+ }
130
194
}
131
195
return nil
132
196
}
0 commit comments