forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailyak.go
103 lines (91 loc) · 2.33 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
package mailyak
import (
"fmt"
"net/smtp"
"regexp"
)
// Emailer defines the interface implemented by MailYak
type Emailer interface {
To(addrs ...string)
Bcc(addrs ...string)
Subject(sub string)
From(addr string)
FromName(name string)
ReplyTo(addr string)
Send() error
HTML() *BodyPart
Plain() *BodyPart
}
// MailYak holds all the necessary information to send an email.
// It also implements the Emailer interface.
type MailYak struct {
html BodyPart
plain BodyPart
toAddrs []string
bccAddrs []string
subject string
fromAddr string
fromName string
replyTo string
attachments []attachment
auth smtp.Auth
trimRegex *regexp.Regexp
host string
}
// New returns an instance of MailYak initialised with the given SMTP address and
// authentication credentials
//
// Note: the host string should include the port (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"),
}
}
// Send attempts to send the built email
//
// Attachments are read when Send() is called, and any errors will be returned
// here.
func (m *MailYak) Send() error {
buf, err := m.buildMime()
if err != nil {
return err
}
err = smtp.SendMail(
m.host,
m.auth,
m.fromAddr,
append(m.toAddrs, m.bccAddrs...),
buf.Bytes(),
)
if err != nil {
return err
}
return nil
}
// String makes MailYak struct printable for debugging purposes (conforms to the Stringer interface).
func (m *MailYak) String() string {
var att []string
for _, a := range m.attachments {
att = append(att, "{filename: "+a.filename+"}")
}
return fmt.Sprintf("&MailYak{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.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
}