-
Notifications
You must be signed in to change notification settings - Fork 16
/
mail.go
169 lines (144 loc) · 3.14 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
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
package main
import (
"crypto/tls"
"errors"
"fmt"
"html/template"
"net/smtp"
"strings"
"time"
)
type unencryptedAuth struct {
smtp.Auth
}
func (a unencryptedAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
s := *server
s.TLS = true
return a.Auth.Start(&s)
}
func setupClient(host, port, username, password string, allowInsecure bool) (*smtp.Client, error) {
var auth smtp.Auth
auth = smtp.PlainAuth("", username, password, host)
if port == "25" {
if !allowInsecure {
return nil, errors.New("sending plain password over unencrypted connection")
}
auth = unencryptedAuth{auth}
}
var tlsconfig *tls.Config
if port != "25" {
// TLS config
tlsconfig = &tls.Config{
InsecureSkipVerify: allowInsecure,
ServerName: host,
}
}
servername := host + ":" + port
var c *smtp.Client
var err error
if port == "465" {
// Here is the key, you need to call tls.Dial instead of smtp.Dial
// for smtp servers running on 465 that require an ssl connection
// from the very beginning (no starttls)
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
return nil, err
}
c, err = smtp.NewClient(conn, host)
if err != nil {
return nil, err
}
} else {
c, err = smtp.Dial(servername)
if err != nil {
return nil, err
}
if port == "587" {
c.StartTLS(tlsconfig)
}
}
// Auth
if err = c.Auth(auth); err != nil {
fmt.Println("here", err)
return nil, err
}
return c, nil
}
func sendMail(from, to, subject, body string, c *smtp.Client) error {
// Setup headers
headers := make(map[string]string)
headers["From"] = from
recipients := strings.Split(to, ",")
recipientsStr := make([]string, 0)
for i := range recipients {
recipientsStr = append(recipientsStr, fmt.Sprintf("<%s>", recipients[i]))
}
headers["To"] = strings.Join(recipientsStr, ",")
headers["Subject"] = subject
// Setup message
message := ""
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + body
// To && From
if err := c.Mail(from); err != nil {
return err
}
if err := c.Rcpt(strings.Join(recipientsStr, ",")); err != nil {
return err
}
// Data
w, err := c.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 nil
}
type mailCtx struct {
NewChunks uint64
ReusedChunks uint64
Datastore string
Error error
Hostname string
StartTime time.Time
EndTime time.Time
}
func (m *mailCtx) Duration() time.Duration {
return m.EndTime.Sub(m.StartTime)
}
func (m *mailCtx) FromattedDuration() string {
return m.Duration().String()
}
func (m *mailCtx) ErrorStr() string {
if m.Error != nil {
return m.Error.Error()
}
return ""
}
func (m *mailCtx) Success() bool {
return m.Error == nil
}
func (m *mailCtx) Status() string {
if m.Success() {
return "Success"
}
return "Failed"
}
func (m *mailCtx) buildStr(txt string) (string, error) {
tmpl, err := template.New("mail").Parse(txt)
if err != nil {
return "", err
}
strBuff := &strings.Builder{}
err = tmpl.Execute(strBuff, m)
return strBuff.String(), err
}