watchtower/pkg/notifications/email.go
Amir Zarrinkafsh dc12a1ac7f
feat: allow hostname override for notifiers (#994)
* feat: allow hostname override for email notifier

As it currently stands all notifiers utilise `os.Hostname` to populate their titles/subjects.

When utilising Docker with a bridged network if you set the hostname for a container to an external DNS hostname Docker's internal DNS resolver will override said hostname for all containers within the bridged network.

This change allows a user to specify what hostname should be represented in the email notifications without having to change the `os.Hostname`.

* feat: allow custom hostname for all notifiers

* docs: adjust notification hostname flag
2021-06-24 00:29:20 +02:00

98 lines
2.7 KiB
Go

package notifications
import (
"time"
"github.com/spf13/cobra"
shoutrrrSmtp "github.com/containrrr/shoutrrr/pkg/services/smtp"
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
)
const (
emailType = "email"
)
type emailTypeNotifier struct {
url string
From, To string
Server, User, Password, SubjectTag string
Port int
tlsSkipVerify bool
entries []*log.Entry
logLevels []log.Level
delay time.Duration
}
// NewEmailNotifier is a factory method creating a new email notifier instance
func NewEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.ConvertibleNotifier {
return newEmailNotifier(c, acceptedLogLevels)
}
func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.ConvertibleNotifier {
flags := c.PersistentFlags()
from, _ := flags.GetString("notification-email-from")
to, _ := flags.GetString("notification-email-to")
server, _ := flags.GetString("notification-email-server")
user, _ := flags.GetString("notification-email-server-user")
password, _ := flags.GetString("notification-email-server-password")
port, _ := flags.GetInt("notification-email-server-port")
tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify")
delay, _ := flags.GetInt("notification-email-delay")
subjecttag, _ := flags.GetString("notification-email-subjecttag")
n := &emailTypeNotifier{
entries: []*log.Entry{},
From: from,
To: to,
Server: server,
User: user,
Password: password,
Port: port,
tlsSkipVerify: tlsSkipVerify,
logLevels: acceptedLogLevels,
delay: time.Duration(delay) * time.Second,
SubjectTag: subjecttag,
}
return n
}
func (e *emailTypeNotifier) GetURL(c *cobra.Command) (string, error) {
conf := &shoutrrrSmtp.Config{
FromAddress: e.From,
FromName: "Watchtower",
ToAddresses: []string{e.To},
Port: uint16(e.Port),
Host: e.Server,
Subject: e.getSubject(c),
Username: e.User,
Password: e.Password,
UseStartTLS: !e.tlsSkipVerify,
UseHTML: false,
Encryption: shoutrrrSmtp.EncMethods.Auto,
Auth: shoutrrrSmtp.AuthTypes.None,
}
if len(e.User) > 0 {
conf.Auth = shoutrrrSmtp.AuthTypes.Plain
}
if e.tlsSkipVerify {
conf.Encryption = shoutrrrSmtp.EncMethods.None
}
return conf.GetURL().String(), nil
}
func (e *emailTypeNotifier) getSubject(c *cobra.Command) string {
subject := GetTitle(c)
if e.SubjectTag != "" {
subject = e.SubjectTag + " " + subject
}
return subject
}