/
mail.go
198 lines (138 loc) · 3.66 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package notifications
import (
"crypto/tls"
"fmt"
"net"
"net/smtp"
"os/exec"
"strconv"
)
// Mail contains all values needed to be able to send a mail
type Mail struct {
Recipient string `long:"recipient" Description:"Email address to which notifications get sent"`
Sender string `long:"sender" Description:"Email address from which notifications get sent"`
SMTPServer string `long:"server" Description:"SMTP server with port for sending mails"`
SMTPSSL bool `long:"ssl" Description:"Whether SSL should be used or not"`
SMTPUser string `long:"user" Description:"User for authenticating the SMTP connection"`
SMTPPassword string `long:"password" Description:"Password for authenticating the SMTP connection"`
}
const sendmail = "/usr/bin/mail"
const newLine = "\r\n"
const subject = "You received a tip"
// SendMail sends a mail
func (mail *Mail) SendMail(amount int64, message string) {
body := "You received a tip of " + strconv.FormatInt(amount, 10) + " satoshis"
if message != "" {
body += " with the message \"" + message + "\""
}
if mail.SMTPServer == "" {
// "mail" command will be used for sending
mail.sendMailCommand(body)
} else {
// SMTP server will be used
mail.sendMailSMTP(body)
}
}
// Sends a mail with the "mail" command
func (mail *Mail) sendMailCommand(body string) {
var cmd *exec.Cmd
if mail.Sender == "" {
cmd = exec.Command(sendmail, "-s", subject, mail.Recipient)
} else {
// Append "From" header
cmd = exec.Command(sendmail, "-s", subject, "-a", "From: "+mail.Sender, mail.Recipient)
}
writer, err := cmd.StdinPipe()
if err == nil {
err = cmd.Start()
if err == nil {
_, err = writer.Write([]byte(body))
if err == nil {
err = writer.Close()
if err == nil {
err = cmd.Wait()
if err == nil {
logSent()
return
}
}
}
}
}
logSendingFailed(err)
}
func (mail *Mail) sendMailSMTP(body string) {
// Because the SMTP method doesn't have a dedicated field for the subject
// it will be in the body of the message
body = "Subject: " + subject + newLine + body
var auth smtp.Auth
host, _, err := net.SplitHostPort(mail.SMTPServer)
if err != nil {
log.Error("Failed to parse host of SMTP server: " + mail.SMTPServer)
return
}
if mail.SMTPUser != "" {
// If there are credentials they are used
auth = smtp.PlainAuth(
"",
mail.SMTPUser,
mail.SMTPPassword,
host,
)
}
if mail.SMTPSSL {
body = "From: " + mail.Sender + newLine + "To: " + mail.Recipient + newLine + body
tlsConfig := &tls.Config{
ServerName: host,
InsecureSkipVerify: true,
}
con, err := tls.Dial("tcp", mail.SMTPServer, tlsConfig)
defer con.Close()
if err != nil {
log.Error("Failed to connect to connect to SMTP server: " + fmt.Sprint(err))
return
}
client, err := smtp.NewClient(con, host)
defer client.Close()
if err != nil {
log.Error("Failed to create SMTP client: " + fmt.Sprint(err))
return
}
err = client.Auth(auth)
if err != nil {
log.Error("Failed to authenticate SMTP client: " + fmt.Sprint(err))
return
}
err = client.Mail(mail.Sender)
err = client.Rcpt(mail.Recipient)
writer, err := client.Data()
defer writer.Close()
if err == nil {
_, err = writer.Write([]byte(body))
}
if err == nil {
logSent()
} else {
logSendingFailed(err)
}
} else {
err := smtp.SendMail(
mail.SMTPServer,
auth,
mail.Sender,
[]string{mail.Recipient},
[]byte(body),
)
if err == nil {
logSent()
} else {
logSendingFailed(err)
}
}
}
func logSent() {
log.Debug("Sent email")
}
func logSendingFailed(err error) {
log.Error("Failed to send mail: " + fmt.Sprint(err))
}