Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SMTPS when using report -to-email #991

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -86,6 +87,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 @@ -112,20 +168,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