forked from go-gomail/gomail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgomail.go
314 lines (271 loc) · 7.18 KB
/
gomail.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Package gomail provides a simple interface to send emails.
//
// More info on Github: https://github.com/go-gomail/gomail
package gomail
import (
"bytes"
"io"
"io/ioutil"
"mime"
"path/filepath"
"time"
"gopkg.in/alexcesaro/quotedprintable.v2"
)
// Message represents an email.
type Message struct {
header header
parts []part
attachments []*File
embedded []*File
charset string
encoding Encoding
hEncoder *quotedprintable.HeaderEncoder
}
type header map[string][]string
type part struct {
contentType string
body *bytes.Buffer
}
// NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding
// by default.
func NewMessage(settings ...MessageSetting) *Message {
msg := &Message{
header: make(header),
charset: "UTF-8",
encoding: QuotedPrintable,
}
msg.applySettings(settings)
var e quotedprintable.Encoding
if msg.encoding == Base64 {
e = quotedprintable.B
} else {
e = quotedprintable.Q
}
msg.hEncoder = e.NewHeaderEncoder(msg.charset)
return msg
}
func (msg *Message) applySettings(settings []MessageSetting) {
for _, s := range settings {
s(msg)
}
}
// A MessageSetting can be used as an argument in NewMessage to configure an
// email.
type MessageSetting func(msg *Message)
// SetCharset is a message setting to set the charset of the email.
//
// Example:
//
// msg := gomail.NewMessage(SetCharset("ISO-8859-1"))
func SetCharset(charset string) MessageSetting {
return func(msg *Message) {
msg.charset = charset
}
}
// SetEncoding is a message setting to set the encoding of the email.
//
// Example:
//
// msg := gomail.NewMessage(SetEncoding(gomail.Base64))
func SetEncoding(enc Encoding) MessageSetting {
return func(msg *Message) {
msg.encoding = enc
}
}
// Encoding represents a MIME encoding scheme like quoted-printable or base64.
type Encoding string
const (
// QuotedPrintable represents the quoted-printable encoding as defined in
// RFC 2045.
QuotedPrintable Encoding = "quoted-printable"
// Base64 represents the base64 encoding as defined in RFC 2045.
Base64 Encoding = "base64"
// Unencoded can be used to avoid encoding the body of an email. The headers
// will still be encoded using quoted-printable encoding.
Unencoded Encoding = "8bit"
)
// SetHeader sets a value to the given header field.
func (msg *Message) SetHeader(field string, value ...string) {
for i := range value {
value[i] = encodeHeader(msg.hEncoder, value[i])
}
msg.header[field] = value
}
// SetHeaders sets the message headers.
//
// Example:
//
// msg.SetHeaders(map[string][]string{
// "From": {"[email protected]"},
// "To": {"[email protected]", "[email protected]"},
// "Subject": {"Hello"},
// })
func (msg *Message) SetHeaders(h map[string][]string) {
for k, v := range h {
msg.SetHeader(k, v...)
}
}
// SetAddressHeader sets an address to the given header field.
func (msg *Message) SetAddressHeader(field, address, name string) {
msg.header[field] = []string{msg.FormatAddress(address, name)}
}
// FormatAddress formats an address and a name as a valid RFC 5322 address.
func (msg *Message) FormatAddress(address, name string) string {
buf := new(bytes.Buffer)
if !quotedprintable.NeedsEncoding(name) {
quote(buf, name)
} else {
var n string
if hasSpecials(name) {
n = encodeHeader(quotedprintable.B.NewHeaderEncoder(msg.charset), name)
} else {
n = encodeHeader(msg.hEncoder, name)
}
buf.WriteString(n)
}
buf.WriteString(" <")
buf.WriteString(address)
buf.WriteByte('>')
return buf.String()
}
// SetDateHeader sets a date to the given header field.
func (msg *Message) SetDateHeader(field string, date time.Time) {
msg.header[field] = []string{msg.FormatDate(date)}
}
// FormatDate formats a date as a valid RFC 5322 date.
func (msg *Message) FormatDate(date time.Time) string {
return date.Format(time.RFC1123Z)
}
// GetHeader gets a header field.
func (msg *Message) GetHeader(field string) []string {
return msg.header[field]
}
// DelHeader deletes a header field.
func (msg *Message) DelHeader(field string) {
delete(msg.header, field)
}
// SetBody sets the body of the message.
func (msg *Message) SetBody(contentType, body string) {
msg.parts = []part{
part{
contentType: contentType,
body: bytes.NewBufferString(body),
},
}
}
// AddAlternative adds an alternative body to the message. Commonly used to
// send HTML emails that default to the plain text version for backward
// compatibility.
//
// Example:
//
// msg.SetBody("text/plain", "Hello!")
// msg.AddAlternative("text/html", "<p>Hello!</p>")
//
// More info: http://en.wikipedia.org/wiki/MIME#Alternative
func (msg *Message) AddAlternative(contentType, body string) {
msg.parts = append(msg.parts,
part{
contentType: contentType,
body: bytes.NewBufferString(body),
},
)
}
// GetBodyWriter gets a writer that writes to the body. It can be useful with
// the templates from packages text/template or html/template.
//
// Example:
//
// w := msg.GetBodyWriter("text/plain")
// t := template.Must(template.New("example").Parse("Hello {{.}}!"))
// t.Execute(w, "Bob")
func (msg *Message) GetBodyWriter(contentType string) io.Writer {
buf := new(bytes.Buffer)
msg.parts = append(msg.parts,
part{
contentType: contentType,
body: buf,
},
)
return buf
}
// A File represents a file that can be attached or embedded in an email.
type File struct {
Name string
MimeType string
Content []byte
ContentID string
}
// OpenFile opens a file on disk to create a gomail.File.
func OpenFile(filename string) (*File, error) {
content, err := readFile(filename)
if err != nil {
return nil, err
}
f := CreateFile(filepath.Base(filename), content)
return f, nil
}
// CreateFile creates a gomail.File from the given name and content.
func CreateFile(name string, content []byte) *File {
mimeType := mime.TypeByExtension(filepath.Ext(name))
if mimeType == "" {
mimeType = "application/octet-stream"
}
return &File{
Name: name,
MimeType: mimeType,
Content: content,
}
}
// Attach attaches the files to the email.
func (msg *Message) Attach(f ...*File) {
if msg.attachments == nil {
msg.attachments = f
} else {
msg.attachments = append(msg.attachments, f...)
}
}
// Embed embeds the images to the email.
//
// Example:
//
// f, err := gomail.OpenFile("/tmp/image.jpg")
// if err != nil {
// panic(err)
// }
// msg.Embed(f)
// msg.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
func (msg *Message) Embed(image ...*File) {
if msg.embedded == nil {
msg.embedded = image
} else {
msg.embedded = append(msg.embedded, image...)
}
}
// Stubbed out for testing.
var readFile = ioutil.ReadFile
func quote(buf *bytes.Buffer, text string) {
buf.WriteByte('"')
for i := 0; i < len(text); i++ {
if text[i] == '\\' || text[i] == '"' {
buf.WriteByte('\\')
}
buf.WriteByte(text[i])
}
buf.WriteByte('"')
}
func hasSpecials(text string) bool {
for i := 0; i < len(text); i++ {
switch c := text[i]; c {
case '(', ')', '<', '>', '[', ']', ':', ';', '@', '\\', ',', '.', '"':
return true
}
}
return false
}
func encodeHeader(enc *quotedprintable.HeaderEncoder, value string) string {
if !quotedprintable.NeedsEncoding(value) {
return value
}
return enc.Encode(value)
}