watchtower/pkg/notifications/email.go

92 lines
2.4 KiB
Go
Raw Normal View History

package notifications
import (
2021-06-27 00:19:52 +02:00
"github.com/spf13/viper"
2018-02-16 11:24:43 +02:00
"time"
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
2019-11-17 11:19:57 +01:00
From, To string
Server, User, Password, SubjectTag string
2019-11-17 11:19:57 +01:00
Port int
tlsSkipVerify bool
entries []*log.Entry
logLevels []log.Level
delay time.Duration
}
2021-06-27 00:19:52 +02:00
func newEmailNotifier() t.ConvertibleNotifier {
from := viper.GetString("notification-email-from")
to := viper.GetString("notification-email-to")
server := viper.GetString("notification-email-server")
user := viper.GetString("notification-email-server-user")
password := viper.GetString("notification-email-server-password")
port := viper.GetInt("notification-email-server-port")
tlsSkipVerify := viper.GetBool("notification-email-server-tls-skip-verify")
delay := viper.GetInt("notification-email-delay")
subjecttag := viper.GetString("notification-email-subjecttag")
2019-06-22 22:04:36 +02:00
n := &emailTypeNotifier{
entries: []*log.Entry{},
2019-06-22 22:04:36 +02:00
From: from,
To: to,
Server: server,
User: user,
Password: password,
Port: port,
tlsSkipVerify: tlsSkipVerify,
delay: time.Duration(delay) * time.Second,
SubjectTag: subjecttag,
}
return n
}
2021-06-27 00:19:52 +02:00
func (e *emailTypeNotifier) GetURL(title string) (string, error) {
conf := &shoutrrrSmtp.Config{
FromAddress: e.From,
FromName: "Watchtower",
ToAddresses: []string{e.To},
Port: uint16(e.Port),
Host: e.Server,
2021-06-27 00:19:52 +02:00
Subject: e.getSubject(title),
Username: e.User,
Password: e.Password,
2021-03-13 08:58:11 +01:00
UseStartTLS: !e.tlsSkipVerify,
UseHTML: false,
2021-03-13 08:58:11 +01:00
Encryption: shoutrrrSmtp.EncMethods.Auto,
Auth: shoutrrrSmtp.AuthTypes.None,
}
if len(e.User) > 0 {
2021-03-13 08:58:11 +01:00
conf.Auth = shoutrrrSmtp.AuthTypes.Plain
}
2021-03-13 08:58:11 +01:00
if e.tlsSkipVerify {
conf.Encryption = shoutrrrSmtp.EncMethods.None
}
2021-03-13 08:58:11 +01:00
return conf.GetURL().String(), nil
}
func (e *emailTypeNotifier) GetDelay() time.Duration {
return e.delay
}
2021-06-27 00:19:52 +02:00
func (e *emailTypeNotifier) getSubject(title string) string {
2021-03-13 08:58:11 +01:00
if e.SubjectTag != "" {
return e.SubjectTag + " " + title
}
return title
2021-03-13 08:58:11 +01:00
}