forked from rakutentech/go-alertnotification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
121 lines (108 loc) · 2.71 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
package alertnotification
import (
"encoding/base64"
"errors"
"fmt"
"net/smtp"
"os"
"strings"
)
// EmailConfig is email setting struct
type EmailConfig struct {
Username string
Password string
Host string
Port string
Sender string
Receivers []string // Can use comma for multiple email
ErrorObj error
Expandos *Expandos // can modify mail subject and content on demand
}
func getReceivers() []string {
delimeter := ","
receivers := os.Getenv("EMAIL_RECEIVERS")
if len(receivers) == 0 {
return nil
}
return strings.Split(receivers, delimeter)
}
// NewEmailConfig create new EmailConfig struct
func NewEmailConfig(err error, expandos *Expandos) EmailConfig {
config := EmailConfig{
Username: os.Getenv("EMAIL_USERNAME"),
Password: os.Getenv("EMAIL_PASSWORD"),
Host: os.Getenv("SMTP_HOST"),
Port: os.Getenv("SMTP_PORT"),
Sender: os.Getenv("EMAIL_SENDER"),
Receivers: getReceivers(),
ErrorObj: err,
Expandos: expandos,
}
return config
}
// Send Alert email
func (ec *EmailConfig) Send() error {
fmt.Println("sending email ....")
var err error
if ec.Receivers == nil {
return errors.New("notification receivers are empty")
}
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
messageDetail := "Error: \r\n" + fmt.Sprintf("%+v", ec.ErrorObj)
subject := os.Getenv("EMAIL_SUBJECT")
// update body and subject dynamically
if ec.Expandos != nil {
if ec.Expandos.EmailBody != "" {
messageDetail = ec.Expandos.EmailBody
}
if ec.Expandos.EmailSubject != "" {
subject = ec.Expandos.EmailSubject
}
}
message := "To: " + strings.Join(ec.Receivers, ", ") + "\r\n" +
"From: " + ec.Sender + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" + base64.StdEncoding.EncodeToString([]byte(string(messageDetail)))
if len(strings.TrimSpace(ec.Username)) != 0 {
stmpAuth := smtp.PlainAuth("", ec.Username, ec.Password, ec.Host)
err = smtp.SendMail(
ec.Host+":"+ec.Port,
stmpAuth,
ec.Sender,
ec.Receivers,
[]byte(message),
)
return err
}
fmt.Println("Send with localhost. ......")
conn, err := smtp.Dial(ec.Host + ":" + ec.Port)
if err != nil {
return err
}
defer conn.Close()
if err = conn.Mail(r.Replace(ec.Sender)); err != nil {
return err
}
// format receiver email
for i := range ec.Receivers {
ec.Receivers[i] = r.Replace(ec.Receivers[i])
if err = conn.Rcpt(ec.Receivers[i]); err != nil {
return err
}
}
w, err := conn.Data()
if err != nil {
return err
}
_, err = w.Write([]byte(message))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return conn.Quit()
}