Skip to content

Commit

Permalink
feat: use gomail for shoutrrr SMTP (#556)
Browse files Browse the repository at this point in the history
* feat: use gomail for shoutrrr SMTP

* fix: use the smtp creds from the shoutrrr URL
  • Loading branch information
adityathebe authored Sep 18, 2023
1 parent daeabdc commit 7db359b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
26 changes: 20 additions & 6 deletions mail/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var FromAddress string

type Mail struct {
message *gomail.Message
dialer *gomail.Dialer
}

func New(to, subject, body, contentType string) *Mail {
Expand All @@ -22,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)
}
24 changes: 23 additions & 1 deletion notification/shoutrrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"os"
"strconv"
"strings"

stripmd "github.com/adityathebe/go-strip-markdown/v2"
Expand Down Expand Up @@ -88,11 +89,32 @@ 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)
}

// NOTE: Until shoutrrr fixes the "UseHTML" props, we'll use the mailer package
if service == "smtp" {
parsedURL, err := url.Parse(shoutrrrURL)
if err != nil {
return fmt.Errorf("failed to parse shoutrrr URL: %w", err)
}

query := parsedURL.Query()
var (
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).
SetCredentials(parsedURL.Hostname(), port, parsedURL.User.Username(), password)
return m.Send()
}

sendErrors := sender.Send(message, params)
for _, err := range sendErrors {
if err != nil {
Expand Down

0 comments on commit 7db359b

Please sign in to comment.