Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b37df89

Browse files
authoredJun 5, 2020
Support SMTPS when using report -to-email (future-architect#991)
* Add smtps func * Add SMTPS implementation * fix error message
1 parent d18e7a7 commit b37df89

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed
 

‎report/email.go

+78-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package report
22

33
import (
4+
"crypto/tls"
45
"fmt"
56
"net"
67
"net/mail"
@@ -87,6 +88,61 @@ type emailSender struct {
8788
send func(string, smtp.Auth, string, []string, []byte) error
8889
}
8990

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+
90146
func (e *emailSender) Send(subject, body string) (err error) {
91147
emailConf := e.conf
92148
to := strings.Join(emailConf.To[:], ", ")
@@ -113,20 +169,28 @@ func (e *emailSender) Send(subject, body string) (err error) {
113169
smtpServer := net.JoinHostPort(emailConf.SMTPAddr, emailConf.SMTPPort)
114170

115171
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+
}
130194
}
131195
return nil
132196
}

0 commit comments

Comments
 (0)
Failed to load comments.