summaryrefslogtreecommitdiff
path: root/internal/notifier/email.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/notifier/email.go')
-rw-r--r--internal/notifier/email.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/internal/notifier/email.go b/internal/notifier/email.go
new file mode 100644
index 0000000..4ce8d69
--- /dev/null
+++ b/internal/notifier/email.go
@@ -0,0 +1,41 @@
+package notifier
+
+import (
+ "fmt"
+ "log"
+ "net/smtp"
+
+ "codeberg.org/snonux/gorum/internal/config"
+)
+
+func emailNotify(conf config.Config, subject, body string) error {
+ if !conf.EmailNotifycationEnabled() {
+ return nil
+ }
+ log.Println("notify:", subject, body)
+
+ headers := map[string]string{
+ "From": conf.EmailFrom,
+ "To": conf.EmailTo,
+ "Subject": subject,
+ "MIME-Version": "1.0",
+ "Content-Type": "text/plain; charset=\"utf-8\"",
+ }
+
+ header := ""
+ for k, v := range headers {
+ header += fmt.Sprintf("%s: %s\r\n", k, v)
+ }
+
+ message := header + "\r\n" + body
+ log.Println("Using SMTP server", conf.SMTPServer)
+
+ return smtp.SendMail(conf.SMTPServer, nil, conf.EmailFrom,
+ []string{conf.EmailTo}, []byte(message))
+}
+
+func emailNotifyError(conf config.Config, err error) {
+ if err := emailNotify(conf, fmt.Sprintf("GORUM: An error occured: %v", err), err.Error()); err != nil {
+ log.Println("error:", err)
+ }
+}