-
Notifications
You must be signed in to change notification settings - Fork 209
/
mailer.go
139 lines (113 loc) · 3.18 KB
/
mailer.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
package authboss
import (
"bytes"
"fmt"
"io"
"net/smtp"
"strings"
"text/template"
)
// SendMail uses the currently configured mailer to deliver e-mails.
func (a *Authboss) SendMail(data Email) error {
return a.Mailer.Send(data)
}
// Mailer is a type that is capable of sending an e-mail.
type Mailer interface {
Send(Email) error
}
// LogMailer creates a mailer that doesn't deliver e-mails but
// simply logs them.
func LogMailer(writer io.Writer) Mailer {
return logMailer{writer}
}
// SMTPMailer creates an SMTP Mailer to send emails with.
func SMTPMailer(server string, auth smtp.Auth) Mailer {
if len(server) == 0 {
panic("SMTP Mailer must be created with a server string.")
}
return smtpMailer{server, auth}
}
// Email all the things. The ToNames and friends are parallel arrays and must
// be 0-length or the same length as their counterpart. To omit a name
// for a user at an index in To simply use an empty string at that
// index in ToNames.
type Email struct {
To, Cc, Bcc []string
ToNames, CcNames, BccNames []string
FromName, From string
ReplyToName, ReplyTo string
Subject string
TextBody string
HTMLBody string
}
type logMailer struct {
io.Writer
}
func (l logMailer) Send(data Email) error {
buf := &bytes.Buffer{}
err := emailTmpl.Execute(buf, data)
if err != nil {
return err
}
toSend := bytes.Replace(buf.Bytes(), []byte{'\n'}, []byte{'\r', '\n'}, -1)
_, err = l.Write(toSend)
return err
}
type smtpMailer struct {
Server string
Auth smtp.Auth
}
func (s smtpMailer) Send(data Email) error {
buf := &bytes.Buffer{}
err := emailTmpl.Execute(buf, data)
if err != nil {
return err
}
toSend := bytes.Replace(buf.Bytes(), []byte{'\n'}, []byte{'\r', '\n'}, -1)
return smtp.SendMail(s.Server, s.Auth, data.From, data.To, toSend)
}
func namedAddress(name, address string) string {
if len(name) == 0 {
return address
}
return fmt.Sprintf("%s <%s>", name, address)
}
func namedAddresses(names, addresses []string) string {
if len(names) == 0 {
return strings.Join(addresses, ", ")
}
buf := &bytes.Buffer{}
first := true
for i, address := range addresses {
if first {
first = false
} else {
buf.WriteString(", ")
}
buf.WriteString(namedAddress(names[i], address))
}
return buf.String()
}
var emailTmpl = template.Must(template.New("email").Funcs(template.FuncMap{
"join": strings.Join,
"namedAddress": namedAddress,
"namedAddresses": namedAddresses,
}).Parse(`To: {{namedAddresses .ToNames .To}}{{if .Cc}}
Cc: {{namedAddresses .CcNames .Cc}}{{end}}{{if .Bcc}}
Bcc: {{namedAddresses .BccNames .Bcc}}{{end}}
From: {{namedAddress .FromName .From}}
Subject: {{.Subject}}{{if .ReplyTo}}
Reply-To: {{namedAddress .ReplyToName .ReplyTo}}{{end}}
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="===============284fad24nao8f4na284f2n4=="
Content-Transfer-Encoding: 7bit
--===============284fad24nao8f4na284f2n4==
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
{{.TextBody}}
--===============284fad24nao8f4na284f2n4==
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
{{.HTMLBody}}
--===============284fad24nao8f4na284f2n4==--
`))