Skip to content

Commit

Permalink
Support SMTPS when using report -to-email (#991)
Browse files Browse the repository at this point in the history
* Add smtps func

* Add SMTPS implementation

* fix error message
  • Loading branch information
shopper29 authored Jun 5, 2020
1 parent d18e7a7 commit b37df89
Showing 1 changed file with 78 additions and 14 deletions.
92 changes: 78 additions & 14 deletions report/email.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package report

import (
"crypto/tls"
"fmt"
"net"
"net/mail"
Expand Down Expand Up @@ -87,6 +88,61 @@ type emailSender struct {
send func(string, smtp.Auth, string, []string, []byte) error
}

func smtps(emailConf config.SMTPConf, message string) (err error) {
auth := smtp.PlainAuth("",
emailConf.User,
emailConf.Password,
emailConf.SMTPAddr,
)

//TLS Config
tlsConfig := &tls.Config{
ServerName: emailConf.SMTPAddr,
}

smtpServer := net.JoinHostPort(emailConf.SMTPAddr, emailConf.SMTPPort)
//New TLS connection
con, err := tls.Dial("tcp", smtpServer, tlsConfig)
if err != nil {
return xerrors.Errorf("Failed to create TLS connection: %w", err)
}
defer con.Close()

c, err := smtp.NewClient(con, emailConf.SMTPAddr)
if err != nil {
return xerrors.Errorf("Failed to create new client: %w", err)
}
if err = c.Auth(auth); err != nil {
return xerrors.Errorf("Failed to authenticate: %w", err)
}
if err = c.Mail(emailConf.From); err != nil {
return xerrors.Errorf("Failed to send Mail command: %w", err)
}
for _, to := range emailConf.To {
if err = c.Rcpt(to); err != nil {
return xerrors.Errorf("Failed to send Rcpt command: %w", err)
}
}

w, err := c.Data()
if err != nil {
return xerrors.Errorf("Failed to send Data command: %w", err)
}
_, err = w.Write([]byte(message))
if err != nil {
return xerrors.Errorf("Failed to write EMail message: %w", err)
}
err = w.Close()
if err != nil {
return xerrors.Errorf("Failed to close Writer: %w", err)
}
err = c.Quit()
if err != nil {
return xerrors.Errorf("Failed to close connection: %w", err)
}
return nil
}

func (e *emailSender) Send(subject, body string) (err error) {
emailConf := e.conf
to := strings.Join(emailConf.To[:], ", ")
Expand All @@ -113,20 +169,28 @@ func (e *emailSender) Send(subject, body string) (err error) {
smtpServer := net.JoinHostPort(emailConf.SMTPAddr, emailConf.SMTPPort)

if emailConf.User != "" && emailConf.Password != "" {
err = e.send(
smtpServer,
smtp.PlainAuth(
"",
emailConf.User,
emailConf.Password,
emailConf.SMTPAddr,
),
emailConf.From,
mailAddresses,
[]byte(message),
)
if err != nil {
return xerrors.Errorf("Failed to send emails: %w", err)
switch emailConf.SMTPPort {
case "465":
err := smtps(emailConf, message)
if err != nil {
return xerrors.Errorf("Failed to send emails: %w", err)
}
default:
err = e.send(
smtpServer,
smtp.PlainAuth(
"",
emailConf.User,
emailConf.Password,
emailConf.SMTPAddr,
),
emailConf.From,
mailAddresses,
[]byte(message),
)
if err != nil {
return xerrors.Errorf("Failed to send emails: %w", err)
}
}
return nil
}
Expand Down

0 comments on commit b37df89

Please sign in to comment.