diff --git a/mail/mailer.go b/mail/mailer.go index e7014a1e7..2e2588ecd 100644 --- a/mail/mailer.go +++ b/mail/mailer.go @@ -11,11 +11,7 @@ var FromAddress string type Mail struct { message *gomail.Message -} - -func (t *Mail) SetFrom(from string) *Mail { - t.message.SetHeader("From", from) - return t + dialer *gomail.Dialer } func New(to, subject, body, contentType string) *Mail { @@ -27,11 +23,24 @@ func New(to, subject, body, contentType string) *Mail { return &Mail{message: m} } +func (t *Mail) SetFrom(from string) *Mail { + t.message.SetHeader("From", from) + return t +} + +func (t *Mail) SetCredentials(host string, port int, user, password string) *Mail { + t.dialer = gomail.NewDialer(host, port, user, password) + return t +} + func (m Mail) Send() error { - host := os.Getenv("SMTP_HOST") - user := os.Getenv("SMTP_USER") - password := os.Getenv("SMTP_PASSWORD") - port, _ := strconv.Atoi(os.Getenv("SMTP_PORT")) - d := gomail.NewDialer(host, port, user, password) - return d.DialAndSend(m.message) + if m.dialer == nil { + host := os.Getenv("SMTP_HOST") + user := os.Getenv("SMTP_USER") + password := os.Getenv("SMTP_PASSWORD") + port, _ := strconv.Atoi(os.Getenv("SMTP_PORT")) + m.SetCredentials(host, port, user, password) + } + + return m.dialer.DialAndSend(m.message) } diff --git a/notification/shoutrrr.go b/notification/shoutrrr.go index ce0ff5d28..7890fbed0 100644 --- a/notification/shoutrrr.go +++ b/notification/shoutrrr.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" "os" + "strconv" "strings" stripmd "github.com/adityathebe/go-strip-markdown/v2" @@ -88,11 +89,9 @@ func Send(ctx *api.Context, connectionName, shoutrrrURL, title, message string, injectTitleIntoProperties(service, title, allProps) - var params *types.Params + params := &types.Params{} if properties != nil { params = (*types.Params)(&allProps) - } else { - params = &types.Params{} } // NOTE: Until shoutrrr fixes the "UseHTML" props, we'll use the mailer package @@ -104,11 +103,15 @@ func Send(ctx *api.Context, connectionName, shoutrrrURL, title, message string, query := parsedURL.Query() var ( - to = utils.Coalesce(query.Get("ToAddresses"), (*params)["ToAddresses"]) - from = utils.Coalesce(query.Get("FromAddress"), (*params)["FromAddress"]) + to = utils.Coalesce(query.Get("ToAddresses"), (*params)["ToAddresses"]) + from = utils.Coalesce(query.Get("FromAddress"), (*params)["FromAddress"]) + password, _ = parsedURL.User.Password() + port, _ = strconv.Atoi(parsedURL.Port()) ) - m := mail.New(to, title, message, `text/html; charset="UTF-8"`).SetFrom(from) + m := mail.New(to, title, message, `text/html; charset="UTF-8"`). + SetFrom(from). + SetCredentials(parsedURL.Hostname(), port, parsedURL.User.Username(), password) return m.Send() }