-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.go
104 lines (89 loc) · 2.81 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"fmt"
"github.com/hako/durafmt"
log "github.com/sirupsen/logrus"
"gopkg.in/gomail.v2"
"html/template"
"strings"
"time"
)
type MailPolicy uint
const (
Never MailPolicy = iota
Always
OnError
)
var MailPolicyToString = map[MailPolicy]string{
Never: "NEVER",
Always: "ALWAYS",
OnError: "ONERROR",
}
func (m MailPolicy) String() string {
return MailPolicyToString[m]
}
func (m *MailPolicy) Decode(value string) error {
for k, v := range MailPolicyToString {
if v == strings.ToUpper(value) {
*m = k
return nil
}
}
return fmt.Errorf("unknown value '%s' for MailPolicy, please use one of 'never, always, onerror'", value)
}
type MailConfig struct {
SmtpHost string `required:"true" envconfig:"smtp_host"`
SmtpPort int `required:"true" envconfig:"smtp_port"`
SmtpUser string `required:"true" envconfig:"smtp_user"`
SmtpPassword string `required:"true" envconfig:"smtp_password"`
MailTo string `required:"true" envconfig:"mail_to"`
MailFrom string `required:"true" envconfig:"mail_from"`
MailPolicy MailPolicy `default:"never" envconfig:"mail_policy"`
}
func (m MailConfig) String() string {
return fmt.Sprintf("mail config [host=%s, port=%d, user=%s, mailTo=%s, mailFrom=%s, mailPolicy=%s]", m.SmtpHost, m.SmtpPort, m.SmtpUser, m.MailTo, m.MailFrom, m.MailPolicy)
}
type MailParams struct {
ContainerName string
ReturnCode int64
Duration time.Duration
StdOut string
StdErr string
}
func (mp MailParams) ShortDuration() string {
return durafmt.Parse(mp.Duration.Truncate(time.Second)).String()
}
func newTemplate() *template.Template {
return template.Must(template.New("mail-body").Parse(`
<p>
📦 Container: <b>{{.ContainerName}}</b>,
Execution: return code 🗠<b>{{.ReturnCode}}</b> in ⏱️ <b>{{.ShortDuration}}</b>,
</p>
📝 stdOut: <pre>{{.StdOut}}</pre>
📝 stdErr: <pre style="color: #a13d3d">{{.StdErr}}</pre>
`))
}
func createTopic(params MailParams) string {
if params.ReturnCode == 0 {
return fmt.Sprintf("[SUCCESS] ✔️ '%s' finished in %s", params.ContainerName, params.ShortDuration())
}
return fmt.Sprintf("[FAIL] ❌ '%s' failed in %s", params.ContainerName, params.ShortDuration())
}
func SendMail(config *MailConfig, params MailParams) error {
msg := gomail.NewMessage()
msg.SetHeader("From", config.MailFrom)
msg.SetHeader("To", config.MailTo)
msg.SetHeader("Subject", createTopic(params))
buf := bytes.NewBuffer(nil)
err := newTemplate().Execute(buf, params)
if err != nil {
log.Error("error during template processing", err)
}
msg.SetBody("text/html", buf.String())
d := gomail.NewDialer(config.SmtpHost, config.SmtpPort, config.SmtpUser, config.SmtpPassword)
if err := d.DialAndSend(msg); err != nil {
return err
}
return nil
}