-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp.go
93 lines (81 loc) · 1.72 KB
/
smtp.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
package mail
import (
"fmt"
"net/smtp"
"github.com/jbchouinard/mxremind/pkg/config"
)
type SmtpClient struct {
username string
smtpClient *smtp.Client
}
func ConnectSmtp(conf *config.ServerConfig) (*SmtpClient, error) {
client, err := smtp.Dial(fmt.Sprintf("%v:%v", conf.Host, conf.Port))
if err != nil {
return nil, err
}
if conf.Tls.Enabled {
err = client.StartTLS(conf.TlsConfig())
if err != nil {
return nil, err
}
}
if conf.Authenticated {
err = client.Auth(smtp.PlainAuth("", conf.Address, conf.Password, conf.Host))
if err != nil {
return nil, err
}
}
return &SmtpClient{conf.Address, client}, nil
}
func MakeMessage(from *string, to *string, subject *string, body *string) string {
return fmt.Sprintf(
"From: %v\r\n"+
"To: %v\r\n"+
"Subject: %v\r\n"+
"\r\n"+
"%v\r\n",
*from,
*to,
*subject,
*body,
)
}
func (client *SmtpClient) Send(to string, subject string, body string) error {
message := MakeMessage(&client.username, &to, &subject, &body)
err := client.smtpClient.Mail(client.username)
if err != nil {
return err
}
err = client.smtpClient.Rcpt(to)
if err != nil {
return err
}
w, err := client.smtpClient.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
}
func (client *SmtpClient) Quit() error {
return client.smtpClient.Quit()
}
// SmtpSender uses a new connection for every e-mail.
type SmtpSender struct {
Conf *config.ServerConfig
}
func (s *SmtpSender) Send(to string, subject string, body string) error {
client, err := ConnectSmtp(s.Conf)
if err != nil {
return err
}
defer client.Quit()
return client.Send(to, subject, body)
}