/
mailyak.go
137 lines (125 loc) · 3.13 KB
/
mailyak.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
package mailyak
import (
"bytes"
"fmt"
"net/smtp"
"regexp"
"strings"
"time"
)
// TODO: in the future, when aliasing is supported or we're making a breaking
// API change anyway, change the MailYak struct name to Email.
// MailYak represents an email.
type MailYak struct {
html BodyPart
plain BodyPart
toAddrs []string
ccAddrs []string
bccAddrs []string
subject string
fromAddr string
fromName string
replyTo string
headers map[string]string // arbitrary headers
attachments []attachment
auth smtp.Auth
trimRegex *regexp.Regexp
host string
writeBccHeader bool
date string
}
// New returns an instance of MailYak using host as the SMTP server, and
// authenticating with auth where required.
//
// host must include the port number (i.e. "smtp.itsallbroken.com:25")
//
// mail := mailyak.New("smtp.itsallbroken.com:25", smtp.PlainAuth(
// "",
// "username",
// "password",
// "stmp.itsallbroken.com",
// ))
//
func New(host string, auth smtp.Auth) *MailYak {
return &MailYak{
headers: map[string]string{},
host: host,
auth: auth,
trimRegex: regexp.MustCompile("\r?\n"),
writeBccHeader: false,
date: time.Now().Format(time.RFC1123Z),
}
}
// Send attempts to send the built email via the configured SMTP server.
//
// Attachments are read when Send() is called, and any connection/authentication
// errors will be returned by Send().
func (m *MailYak) Send() error {
buf, err := m.buildMime()
if err != nil {
return err
}
return smtp.SendMail(
m.host,
m.auth,
m.fromAddr,
append(append(m.toAddrs, m.ccAddrs...), m.bccAddrs...),
buf.Bytes(),
)
}
// MimeBuf returns the buffer containing all the RAW MIME data.
//
// MimeBuf is typically used with an API service such as Amazon SES that does
// not use an SMTP interface.
func (m *MailYak) MimeBuf() (*bytes.Buffer, error) {
buf, err := m.buildMime()
if err != nil {
return nil, err
}
return buf, nil
}
// String returns a redacted description of the email state, typically for
// logging or debugging purposes.
//
// Authentication information is not included in the returned string.
func (m *MailYak) String() string {
var (
att []string
custom string
)
for _, a := range m.attachments {
att = append(att, "{filename: "+a.filename+"}")
}
if len(m.headers) > 0 {
var hdrs []string
for k, v := range m.headers {
hdrs = append(hdrs, fmt.Sprintf("%s: %q", k, v))
}
custom = strings.Join(hdrs, ", ") + ", "
}
return fmt.Sprintf(
"&MailYak{date: %q, from: %q, fromName: %q, html: %v bytes, plain: %v bytes, toAddrs: %v, "+
"bccAddrs: %v, subject: %q, %vhost: %q, attachments (%v): %v, auth set: %v}",
m.date,
m.fromAddr,
m.fromName,
len(m.HTML().String()),
len(m.Plain().String()),
m.toAddrs,
m.bccAddrs,
m.subject,
custom,
m.host,
len(att),
att,
m.auth != nil,
)
}
// HTML returns a BodyPart for the HTML email body.
func (m *MailYak) HTML() *BodyPart {
return &m.html
}
// Plain returns a BodyPart for the plain-text email body.
func (m *MailYak) Plain() *BodyPart {
return &m.plain
}