-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_mail.go
66 lines (55 loc) · 1.83 KB
/
report_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
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"crypto/tls"
"fmt"
"os/user"
"github.com/Showmax/go-fqdn"
"gopkg.in/gomail.v2"
)
// SendReportMail sends a report mail to the recipients configured in the options using the
// configured SMTP server, containing the full log output up until the function call
func SendReportMail(options *Options) {
logContent := Log.String()
if options.ReportOptions.smtpHost == "" ||
options.ReportOptions.smtpPort == 0 {
if len(options.ReportOptions.recipients) > 0 {
Log.Warn.Println("Status mail recipients given, but SMTP configuration is incomplete (host/port missing/invalid).")
} else {
Log.Debug.Println("No SMTP configuration given.")
}
return
}
var from string
if options.ReportOptions.from == "" {
user, err := user.Current()
if err != nil {
panic(fmt.Sprintf("Error obtaining current user (for From: value): %v", err))
}
fqdn, err := fqdn.FqdnHostname()
if err != nil {
panic(fmt.Sprintf("Error obtaining FqdnHostname (for From: value): %v", err))
}
from = fmt.Sprintf("%s@%s", user.Username, fqdn)
} else {
from = options.ReportOptions.from
}
Log.Info.Printf("Sending report mail to: %v", options.ReportOptions.recipients)
logLevel := Log.MaxLogLevel()
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", options.ReportOptions.recipients...)
m.SetHeader("Subject", fmt.Sprintf("rotating-rsync-backup [%s]: %s", logLevel, options.profileName))
m.SetBody("text/plain", logContent)
d := gomail.NewDialer(
options.ReportOptions.smtpHost,
int(options.ReportOptions.smtpPort),
options.ReportOptions.smtpUsername,
options.ReportOptions.smtpPassword,
)
if options.ReportOptions.smtpInsecure {
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
if err := d.DialAndSend(m); err != nil {
panic(fmt.Sprintf("Error while sending report mail: %v", err))
}
}