forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
176 lines (158 loc) · 4.53 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package notifiers
import (
"crypto/tls"
"fmt"
"github.com/go-mail/mail"
"github.com/statping-ng/emails"
"github.com/statping-ng/statping-ng/types/core"
"github.com/statping-ng/statping-ng/types/failures"
"github.com/statping-ng/statping-ng/types/notifications"
"github.com/statping-ng/statping-ng/types/notifier"
"github.com/statping-ng/statping-ng/types/services"
"github.com/statping-ng/statping-ng/utils"
)
var _ notifier.Notifier = (*emailer)(nil)
var (
mailer *mail.Dialer
)
type emailer struct {
*notifications.Notification
}
func (e *emailer) Select() *notifications.Notification {
return e.Notification
}
func (e *emailer) Valid(values notifications.Values) error {
return nil
}
var email = &emailer{¬ifications.Notification{
Method: "email",
Title: "SMTP Mail",
Description: "Send emails via SMTP when services are online or offline.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Icon: "far fa-envelope",
Limits: 30,
Form: []notifications.NotificationForm{{
Type: "text",
Title: "SMTP Host",
Placeholder: "Insert your SMTP Host here.",
DbField: "Host",
}, {
Type: "text",
Title: "SMTP Username",
Placeholder: "Insert your SMTP Username here.",
DbField: "Username",
}, {
Type: "password",
Title: "SMTP Password",
Placeholder: "Insert your SMTP Password here.",
DbField: "Password",
}, {
Type: "number",
Title: "SMTP Port",
Placeholder: "Insert your SMTP Port here.",
DbField: "Port",
}, {
Type: "text",
Title: "Outgoing Email Address",
Placeholder: "[email protected]",
DbField: "Var1",
}, {
Type: "email",
Title: "Send Alerts To",
Placeholder: "[email protected]",
DbField: "Var2",
}, {
Type: "switch",
Title: "Disable TLS/SSL",
Placeholder: "",
SmallText: "Enabling this will set Insecure Skip Verify to true",
DbField: "api_key",
}}},
}
type emailOutgoing struct {
To string
Subject string
Template string
From string
Data replacer
Source string
Sent bool
}
// OnFailure will trigger failing service
func (e *emailer) OnFailure(s services.Service, f failures.Failure) (string, error) {
subscriber := e.Var2.String
subject := fmt.Sprintf("Service %s is Offline", s.Name)
tmpl := renderEmail(s, subscriber, f, emails.Failure)
email := &emailOutgoing{
To: e.Var2.String,
Subject: subject,
Template: tmpl,
From: e.Var1.String,
}
return tmpl, e.dialSend(email)
}
// OnSuccess will trigger successful service
func (e *emailer) OnSuccess(s services.Service) (string, error) {
subscriber := e.Var2.String
subject := fmt.Sprintf("Service %s is Back Online", s.Name)
tmpl := renderEmail(s, subscriber, failures.Failure{}, emails.Success)
email := &emailOutgoing{
To: e.Var2.String,
Subject: subject,
Template: tmpl,
From: e.Var1.String,
}
return tmpl, e.dialSend(email)
}
func renderEmail(s services.Service, subscriber string, f failures.Failure, emailData string) string {
data := replacer{
Core: *core.App,
Service: s,
Failure: f,
Email: subscriber,
Custom: nil,
}
output, err := emails.Parse(emailData, data)
if err != nil {
log.Errorln(err)
return emailData
}
return output
}
// OnTest triggers when this notifier has been saved
func (e *emailer) OnTest() (string, error) {
subscriber := e.Var2.String
service := services.Example(true)
subject := fmt.Sprintf("Service %v is Back Online", service.Name)
email := &emailOutgoing{
To: e.Var2.String,
Subject: subject,
Template: renderEmail(service, subscriber, failures.Example(), emailFailure),
From: e.Var1.String,
}
return subject, e.dialSend(email)
}
// OnSave will trigger when this notifier is saved
func (e *emailer) OnSave() (string, error) {
return "", nil
}
func (e *emailer) dialSend(email *emailOutgoing) error {
mailer = mail.NewDialer(e.Host.String, int(e.Port.Int64), e.Username.String, e.Password.String)
m := mail.NewMessage()
// if email setting TLS is Disabled
if e.ApiKey.String == "true" {
mailer.SSL = false
} else {
mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
m.SetAddressHeader("From", email.From, "Statping")
m.SetHeader("To", email.To)
m.SetHeader("Subject", email.Subject)
m.SetBody("text/html", email.Template)
if err := mailer.DialAndSend(m); err != nil {
utils.Log.Errorln(fmt.Sprintf("email '%v' sent to: %v (size: %v) %v", email.Subject, email.To, len([]byte(email.Source)), err))
return err
}
return nil
}