-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
88 lines (74 loc) · 1.9 KB
/
email.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package wlbdqm
import (
"bytes"
"gopkg.in/gomail.v2"
"os"
"strconv"
"text/template"
)
const (
MailKeyFrom = "WLBDQM_MAIL_FROM"
MailKeyPassword = "WLBDQM_MAIL_PASSWORD"
MailKeyHost = "WLBDQM_MAIL_HOST"
MailKeyPort = "WLBDQM_MAIL_PORT"
)
var (
tplDiskQuotaInsufficient = `<div style="font-size: 18px;">
Dear Boss @T_T@: <br />
您的存储使用占比或文件数量占比已达指定比例 {{.MaxPercentage}}%, 请及时清理磁盘。<br />
存储使用比例:<font color="red">{{.DPercentage}}%</font>,文件数量占比:<font color="red">{{.FPercentage}}%</font><br />
<br />
<div style="padding: 4px;">
{{.DPOutput}}
</div>
<br />
!^_^! Best Wishes.
</div>`
)
// ContentItem
type ContentItem struct {
MaxPercentage float64
DPercentage float64
FPercentage float64
DPOutput string
}
type Message struct {
Subject string
Body string
}
func SendEmail(toList []string, message Message) error {
from := os.Getenv(MailKeyFrom)
password := os.Getenv(MailKeyPassword)
host := os.Getenv(MailKeyHost)
port := os.Getenv(MailKeyPort)
portInt64, err := strconv.ParseInt(port, 10, 64)
if err != nil {
return err
}
m := gomail.NewMessage()
m.SetHeader("From", from)
m.SetHeader("To", toList...)
m.SetHeader("Subject", "程序自动发送,勿回 - "+message.Subject)
m.SetBody("text/html", message.Body)
d := gomail.NewDialer(host, int(portInt64), from, password)
if err := d.DialAndSend(m); err != nil {
return err
}
return nil
}
func PrepareDiskInsufficientContent(item ContentItem) (Message, error) {
t, err := template.New("").Parse(tplDiskQuotaInsufficient)
if err != nil {
return Message{}, err
}
var buffer bytes.Buffer
if err := t.Execute(&buffer, item); err != nil {
return Message{}, err
}
content := buffer.String()
//DebugPrintln(content)
return Message{
Subject: "存储空间不足",
Body: content,
}, nil
}