forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailyak.go
122 lines (111 loc) · 2.8 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
package mailyak
import (
"bytes"
"fmt"
"net/smtp"
"regexp"
"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
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{
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(m.toAddrs, 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
for _, a := range m.attachments {
att = append(att, "{filename: "+a.filename+"}")
}
return fmt.Sprintf(
"&MailYak{date: %q, from: %q, fromName: %q, html: %v bytes, plain: %v bytes, toAddrs: %v, "+
"bccAddrs: %v, subject: %q, host: %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,
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
}