summaryrefslogtreecommitdiff
path: root/internal/notify.go
blob: 7d8fc5ab25a5d9157e3ef9ea294642e34b92ce85 (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
package internal

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

func notify(config config, subject, body string) error {
	log.Println("notify", subject, body)

	headers := make(map[string]string)
	headers["From"] = config.EmailFrom
	headers["To"] = config.EmailTo
	headers["Subject"] = subject
	headers["MIME-Version"] = "1.0"
	headers["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", config.SMTPServer)

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

func notifyError(config config, err error) error {
	return notify(config, fmt.Sprintf("GOGIOS: An error occured: %v", err), err.Error())
}