-
Notifications
You must be signed in to change notification settings - Fork 6
/
email.go
78 lines (60 loc) · 1.49 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
package email
import (
"context"
"fmt"
"net/smtp"
"github.com/copybird/copybird/core"
)
const GROUP_NAME = "global"
const TYPE_NAME = "notifier"
const MODULE_NAME = "email"
type GlobalNotifierEmail struct {
core.Module
Config *Config
}
func (e *GlobalNotifierEmail) GetGroup() core.ModuleGroup {
return GROUP_NAME
}
func (e *GlobalNotifierEmail) GetType() core.ModuleType {
return TYPE_NAME
}
func (e *GlobalNotifierEmail) GetName() string {
return MODULE_NAME
}
func (e *GlobalNotifierEmail) GetConfig() interface{} {
return &Config{}
}
func (e *GlobalNotifierEmail) InitModule(_cfg interface{}) error {
e.Config = _cfg.(*Config)
return nil
}
func (e *GlobalNotifierEmail) Run(ctx context.Context) error {
if err := e.SendEmail(); err != nil {
return err
}
return nil
}
func (e *GlobalNotifierEmail) Close() error {
return nil
}
func (e *GlobalNotifierEmail) SendEmail() error {
from := e.Config.MailerUser
pass := e.Config.MailerPassword
to := e.Config.MailTo
body := "Dump created successfully"
subject := "Dump"
header := ""
header += fmt.Sprintf("From: %s\r\n", from)
header += fmt.Sprintf("To: %s\r\n", to)
header += fmt.Sprint("MIME-Version: 1.0\r\n")
header += fmt.Sprint("Content-type: text/html\r\n")
header += fmt.Sprintf("Subject: %s\r\n", subject)
header += "\r\n" + body + "\r\n"
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(header))
if err != nil {
return err
}
return nil
}