File size: 4,604 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
package email
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/mail"
"net/smtp"
"strings"
"github.com/matcornic/hermes/v2"
"github.com/target/goalert/config"
"github.com/target/goalert/notification"
"github.com/target/goalert/notification/nfydest"
"gopkg.in/gomail.v2"
)
type Sender struct{}
func NewSender(ctx context.Context) *Sender {
return &Sender{}
}
var _ nfydest.MessageSender = &Sender{}
// Send will send an for the provided message type.
func (s *Sender) SendMessage(ctx context.Context, msg notification.Message) (*notification.SentMessage, error) {
cfg := config.FromContext(ctx)
fromAddr, err := mail.ParseAddress(cfg.SMTP.From)
if err != nil {
return nil, err
}
toAddr, err := mail.ParseAddress(msg.DestArg(FieldEmailAddress))
if err != nil {
return nil, err
}
if fromAddr.Name == "" {
fromAddr.Name = cfg.ApplicationName()
}
h := hermes.Hermes{
Product: hermes.Product{
Name: cfg.ApplicationName(),
Link: cfg.General.PublicURL,
Logo: cfg.CallbackURL("/static/goalert-alt-logo.png"),
},
}
var e hermes.Email
var subject string
switch m := msg.(type) {
case notification.Test:
subject = "Test Message"
e.Body.Title = "Test Message"
e.Body.Intros = []string{"This is a test message."}
case notification.Verification:
subject = "Verification Message"
e.Body.Title = "Verification Message"
e.Body.Intros = []string{"This is your contact method verification code."}
e.Body.Actions = []hermes.Action{{
Instructions: "Click the REACTIVATE link on your profile page and enter the verification code.",
InviteCode: m.Code,
}}
case notification.Alert:
subject = fmt.Sprintf("Alert #%d: %s", m.AlertID, m.Summary)
e.Body.Title = fmt.Sprintf("Alert #%d", m.AlertID)
e.Body.Intros = []string{m.Summary, m.Details}
e.Body.Actions = []hermes.Action{{
Button: hermes.Button{
Text: "Open Alert Details",
Link: cfg.CallbackURL(fmt.Sprintf("/alerts/%d", m.AlertID)),
},
}}
case notification.AlertBundle:
subject = fmt.Sprintf("Service %s has %d unacknowledged alerts", m.ServiceName, m.Count)
e.Body.Title = "Multiple Unacknowledged Alerts"
e.Body.Intros = []string{fmt.Sprintf("The service %s has %d unacknowledged alerts.", m.ServiceName, m.Count)}
e.Body.Actions = []hermes.Action{{
Button: hermes.Button{
Text: "Open Alert List",
Link: cfg.CallbackURL(fmt.Sprintf("/services/%s/alerts", m.ServiceID)),
},
}}
case notification.AlertStatus:
subject = fmt.Sprintf("Alert #%d: %s", m.AlertID, m.LogEntry)
e.Body.Title = fmt.Sprintf("Alert #%d", m.AlertID)
e.Body.Intros = []string{m.LogEntry}
e.Body.Actions = []hermes.Action{{
Button: hermes.Button{
Text: "Open Alert Details",
Link: cfg.CallbackURL(fmt.Sprintf("/alerts/%d", m.AlertID)),
},
}}
e.Body.Outros = []string{"You are receiving this message because you have status updates enabled. Visit your Profile page to change this."}
default:
return nil, errors.New("message type not supported")
}
htmlBody, err := h.GenerateHTML(e)
if err != nil {
return nil, err
}
textBody, err := h.GeneratePlainText(e)
if err != nil {
return nil, err
}
g := gomail.NewMessage()
g.SetHeader("From", fromAddr.String())
g.SetAddressHeader("To", toAddr.Address, toAddr.Name)
g.SetHeader("Subject", subject)
g.SetBody("text/plain", textBody)
g.AddAlternative("text/html", htmlBody)
var buf bytes.Buffer
_, err = g.WriteTo(&buf)
if err != nil {
return nil, err
}
host, port, _ := net.SplitHostPort(cfg.SMTP.Address)
if host == "" {
host = cfg.SMTP.Address
}
tlsCfg := &tls.Config{
InsecureSkipVerify: cfg.SMTP.SkipVerify,
ServerName: host,
}
sendFn := SendMailTLS
if cfg.SMTP.DisableTLS {
sendFn = SendMail
if port == "" {
port = "25"
}
} else if port == "" {
port = "465"
}
var authFn NegotiateAuth
if cfg.SMTP.Username+cfg.SMTP.Password != "" {
authFn = func(auths string) smtp.Auth {
if strings.Contains(auths, "CRAM-MD5") {
return smtp.CRAMMD5Auth(cfg.SMTP.Username, cfg.SMTP.Password)
}
if strings.Contains(auths, "PLAIN") {
return smtp.PlainAuth("", cfg.SMTP.Username, cfg.SMTP.Password, host)
}
if strings.Contains(auths, "LOGIN") {
return LoginAuth(cfg.SMTP.Username, cfg.SMTP.Password, host)
}
return nil
}
}
err = sendFn(ctx, net.JoinHostPort(host, port), authFn, fromAddr.Address, []string{toAddr.Address}, buf.Bytes(), tlsCfg)
if err != nil {
return nil, err
}
return ¬ification.SentMessage{
State: notification.StateSent,
SrcValue: fromAddr.String(),
}, nil
}
|