2020-03-12 08:09:25 +00:00
|
|
|
package notifiers
|
2018-07-11 03:06:21 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 22:21:58 +00:00
|
|
|
"crypto/tls"
|
2018-07-11 03:06:21 +00:00
|
|
|
"fmt"
|
2020-10-13 00:31:51 +00:00
|
|
|
|
2018-09-27 03:20:02 +00:00
|
|
|
"github.com/go-mail/mail"
|
2020-08-26 10:48:05 +00:00
|
|
|
"github.com/statping/emails"
|
2020-06-19 09:14:40 +00:00
|
|
|
"github.com/statping/statping/types/core"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/failures"
|
2020-03-14 03:13:20 +00:00
|
|
|
"github.com/statping/statping/types/notifications"
|
|
|
|
"github.com/statping/statping/types/notifier"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
|
|
|
"github.com/statping/statping/utils"
|
2018-07-11 03:06:21 +00:00
|
|
|
)
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
var _ notifier.Notifier = (*emailer)(nil)
|
2020-03-09 15:15:15 +00:00
|
|
|
|
2018-07-11 03:06:21 +00:00
|
|
|
var (
|
2018-09-27 03:20:02 +00:00
|
|
|
mailer *mail.Dialer
|
2018-07-11 03:06:21 +00:00
|
|
|
)
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
type emailer struct {
|
|
|
|
*notifications.Notification
|
2018-07-11 03:06:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
func (e *emailer) Select() *notifications.Notification {
|
|
|
|
return e.Notification
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:48:35 +00:00
|
|
|
func (e *emailer) Valid(values notifications.Values) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
var email = &emailer{¬ifications.Notification{
|
2018-09-15 01:18:21 +00:00
|
|
|
Method: "email",
|
2020-06-21 07:16:13 +00:00
|
|
|
Title: "SMTP Mail",
|
2018-09-28 06:57:03 +00:00
|
|
|
Description: "Send emails via SMTP when services are online or offline.",
|
2018-09-15 01:18:21 +00:00
|
|
|
Author: "Hunter Long",
|
|
|
|
AuthorUrl: "https://github.com/hunterlong",
|
2018-10-21 16:22:26 +00:00
|
|
|
Icon: "far fa-envelope",
|
2020-03-17 03:13:07 +00:00
|
|
|
Limits: 30,
|
2020-03-14 03:13:20 +00:00
|
|
|
Form: []notifications.NotificationForm{{
|
2018-09-15 01:18:21 +00:00
|
|
|
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",
|
2019-01-17 21:11:43 +00:00
|
|
|
Title: "Outgoing Email Address",
|
|
|
|
Placeholder: "outgoing@email.com",
|
2018-09-15 01:18:21 +00:00
|
|
|
DbField: "Var1",
|
|
|
|
}, {
|
|
|
|
Type: "email",
|
|
|
|
Title: "Send Alerts To",
|
2019-01-17 21:11:43 +00:00
|
|
|
Placeholder: "sendto@email.com",
|
2018-09-15 01:18:21 +00:00
|
|
|
DbField: "Var2",
|
2019-04-03 15:34:16 +00:00
|
|
|
}, {
|
2020-06-19 09:14:40 +00:00
|
|
|
Type: "switch",
|
2019-04-03 15:34:16 +00:00
|
|
|
Title: "Disable TLS/SSL",
|
|
|
|
Placeholder: "",
|
2020-06-22 07:13:57 +00:00
|
|
|
SmallText: "Enabling this will set Insecure Skip Verify to true",
|
2019-04-03 15:34:16 +00:00
|
|
|
DbField: "api_key",
|
2020-03-14 03:13:20 +00:00
|
|
|
}}},
|
2018-07-12 06:53:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 05:03:10 +00:00
|
|
|
type emailOutgoing struct {
|
2018-07-17 09:18:20 +00:00
|
|
|
To string
|
|
|
|
Subject string
|
|
|
|
Template string
|
|
|
|
From string
|
2020-05-02 09:51:47 +00:00
|
|
|
Data replacer
|
2018-07-17 09:18:20 +00:00
|
|
|
Source string
|
|
|
|
Sent bool
|
|
|
|
}
|
|
|
|
|
2018-09-15 22:39:17 +00:00
|
|
|
// OnFailure will trigger failing service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (e *emailer) OnFailure(s services.Service, f failures.Failure) (string, error) {
|
2020-10-13 00:31:51 +00:00
|
|
|
subscriber := e.Var2.String
|
2020-05-02 09:51:47 +00:00
|
|
|
subject := fmt.Sprintf("Service %s is Offline", s.Name)
|
2020-10-13 00:31:51 +00:00
|
|
|
tmpl := renderEmail(s, subscriber, f, emails.Failure)
|
2018-10-06 05:03:10 +00:00
|
|
|
email := &emailOutgoing{
|
2020-07-22 19:07:42 +00:00
|
|
|
To: e.Var2.String,
|
2020-05-02 09:51:47 +00:00
|
|
|
Subject: subject,
|
2020-06-19 09:14:40 +00:00
|
|
|
Template: tmpl,
|
2020-07-22 19:07:42 +00:00
|
|
|
From: e.Var1.String,
|
2018-07-12 04:49:18 +00:00
|
|
|
}
|
2020-06-19 09:14:40 +00:00
|
|
|
return tmpl, e.dialSend(email)
|
2018-07-11 03:06:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 22:39:17 +00:00
|
|
|
// OnSuccess will trigger successful service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (e *emailer) OnSuccess(s services.Service) (string, error) {
|
2020-10-13 00:31:51 +00:00
|
|
|
subscriber := e.Var2.String
|
2020-05-02 09:51:47 +00:00
|
|
|
subject := fmt.Sprintf("Service %s is Back Online", s.Name)
|
2020-10-13 00:31:51 +00:00
|
|
|
tmpl := renderEmail(s, subscriber, failures.Failure{}, emails.Success)
|
2020-03-14 03:13:20 +00:00
|
|
|
email := &emailOutgoing{
|
2020-07-22 19:07:42 +00:00
|
|
|
To: e.Var2.String,
|
2020-05-02 09:51:47 +00:00
|
|
|
Subject: subject,
|
2020-06-19 09:14:40 +00:00
|
|
|
Template: tmpl,
|
2020-07-22 19:07:42 +00:00
|
|
|
From: e.Var1.String,
|
2018-09-20 09:46:51 +00:00
|
|
|
}
|
2020-06-19 09:14:40 +00:00
|
|
|
return tmpl, e.dialSend(email)
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:31:51 +00:00
|
|
|
func renderEmail(s services.Service, subscriber string, f failures.Failure, emailData string) string {
|
2020-06-19 09:14:40 +00:00
|
|
|
data := replacer{
|
2020-06-26 04:08:12 +00:00
|
|
|
Core: *core.App,
|
2020-06-19 09:14:40 +00:00
|
|
|
Service: s,
|
|
|
|
Failure: f,
|
2020-10-13 00:31:51 +00:00
|
|
|
Email: subscriber,
|
2020-06-19 09:14:40 +00:00
|
|
|
Custom: nil,
|
|
|
|
}
|
2020-08-26 10:48:05 +00:00
|
|
|
output, err := emails.Parse(emailData, data)
|
|
|
|
if err != nil {
|
2020-06-19 09:14:40 +00:00
|
|
|
log.Errorln(err)
|
2020-08-06 04:17:14 +00:00
|
|
|
return emailData
|
2020-06-19 09:14:40 +00:00
|
|
|
}
|
2020-08-26 10:48:05 +00:00
|
|
|
return output
|
2018-07-11 03:06:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:20:02 +00:00
|
|
|
// OnTest triggers when this notifier has been saved
|
2020-04-11 05:59:51 +00:00
|
|
|
func (e *emailer) OnTest() (string, error) {
|
2020-10-13 00:31:51 +00:00
|
|
|
subscriber := e.Var2.String
|
2020-06-19 09:14:40 +00:00
|
|
|
service := services.Example(true)
|
|
|
|
subject := fmt.Sprintf("Service %v is Back Online", service.Name)
|
2018-10-16 08:30:10 +00:00
|
|
|
email := &emailOutgoing{
|
2020-07-22 19:07:42 +00:00
|
|
|
To: e.Var2.String,
|
2020-04-11 05:59:51 +00:00
|
|
|
Subject: subject,
|
2020-10-13 00:31:51 +00:00
|
|
|
Template: renderEmail(service, subscriber, failures.Example(), emailFailure),
|
2020-07-22 19:07:42 +00:00
|
|
|
From: e.Var1.String,
|
2018-10-16 08:30:10 +00:00
|
|
|
}
|
2020-06-19 09:14:40 +00:00
|
|
|
return subject, e.dialSend(email)
|
2018-09-27 03:20:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 06:52:07 +00:00
|
|
|
// OnSave will trigger when this notifier is saved
|
|
|
|
func (e *emailer) OnSave() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:50:30 +00:00
|
|
|
func (e *emailer) dialSend(email *emailOutgoing) error {
|
2020-07-22 19:07:42 +00:00
|
|
|
mailer = mail.NewDialer(e.Host.String, int(e.Port.Int64), e.Username.String, e.Password.String)
|
2018-09-27 03:20:02 +00:00
|
|
|
m := mail.NewMessage()
|
2019-04-03 15:34:16 +00:00
|
|
|
// if email setting TLS is Disabled
|
2020-07-22 19:07:42 +00:00
|
|
|
if e.ApiKey.String == "true" {
|
2019-04-03 15:34:16 +00:00
|
|
|
mailer.SSL = false
|
|
|
|
} else {
|
|
|
|
mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
2020-06-15 04:46:28 +00:00
|
|
|
|
2020-06-19 09:14:40 +00:00
|
|
|
m.SetAddressHeader("From", email.From, "Statping")
|
2018-07-12 06:53:18 +00:00
|
|
|
m.SetHeader("To", email.To)
|
|
|
|
m.SetHeader("Subject", email.Subject)
|
2020-06-19 09:14:40 +00:00
|
|
|
m.SetBody("text/html", email.Template)
|
2020-03-25 18:46:50 +00:00
|
|
|
|
2018-07-16 07:02:33 +00:00
|
|
|
if err := mailer.DialAndSend(m); err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Errorln(fmt.Sprintf("email '%v' sent to: %v (size: %v) %v", email.Subject, email.To, len([]byte(email.Source)), err))
|
2018-07-12 06:53:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-19 09:14:40 +00:00
|
|
|
|
2018-07-12 06:53:18 +00:00
|
|
|
return nil
|
|
|
|
}
|