summaryrefslogtreecommitdiff
path: root/internal/notifier/email.go
blob: 423b7631acd81850ded1309cac9fae4608021f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package notifier

import (
	"fmt"
	"log"
	"net/smtp"

	"codeberg.org/snonux/gorum/internal/config"
)

type email struct {
	subject, body string
}

func (em email) send(conf config.Config) error {
	if !conf.EmailNotifycationEnabled() {
		return nil
	}
	log.Println("notify:", em.subject, em.body)

	headers := map[string]string{
		"From":         conf.EmailFrom,
		"To":           conf.EmailTo,
		"Subject":      em.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" + em.body
	log.Println("Using SMTP server", conf.SMTPServer)

	return smtp.SendMail(conf.SMTPServer, nil, conf.EmailFrom,
		[]string{conf.EmailTo}, []byte(message))
}