-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.go
55 lines (42 loc) · 1.32 KB
/
mail.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"errors"
"net/smtp"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
)
func buildEMail(timestamp time.Time, from string, to []string, subject string, body string) string {
var msg strings.Builder
msg.WriteString("From: " + from + "\r\n")
msg.WriteString("To: " + strings.Join(to, ";") + "\r\n")
msg.WriteString("Date: " + timestamp.Format(time.RFC1123Z) + "\r\n")
msg.WriteString("Content-Type: text/plain; charset=UTF-8\r\n")
msg.WriteString("Subject: " + subject + "\r\n")
msg.WriteString("\r\n" + body + "\r\n")
return msg.String()
}
func sendMail(timestamp time.Time, message string, title string, errCh chan ReporterError) {
e := ReporterError{
Reporter: "mail",
}
from := config.Reporter.Mail.From
to := []string{config.Reporter.Mail.To}
username := config.Reporter.Mail.User
password := config.Reporter.Mail.Password
host := config.Reporter.Mail.Host
port := strconv.Itoa(config.Reporter.Mail.Port)
address := host + ":" + port
subject := title
body := message
mail := buildEMail(timestamp, from, to, subject, body)
auth := smtp.PlainAuth("", username, password, host)
err := smtp.SendMail(address, auth, from, to, []byte(mail))
if err != nil {
log.Error().Err(err).Str("reporter", "Mail").Msg("")
e.Error = errors.New("failed to send mail")
errCh <- e
return
}
}