| 1 |
package mailyak |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/tls" |
| 6 |
"fmt" |
| 7 |
"net/mail" |
| 8 |
"net/smtp" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
// MailYak is an easy-to-use email builder. |
| 15 |
type MailYak struct { |
| 16 |
html BodyPart |
| 17 |
plain BodyPart |
| 18 |
|
| 19 |
localName string |
| 20 |
toAddrs []string |
| 21 |
ccAddrs []string |
| 22 |
bccAddrs []string |
| 23 |
subject string |
| 24 |
fromAddr string |
| 25 |
fromName string |
| 26 |
replyTo string |
| 27 |
headers map[string][]string // arbitrary headers |
| 28 |
attachments []attachment |
| 29 |
trimRegex *regexp.Regexp |
| 30 |
auth smtp.Auth |
| 31 |
host string |
| 32 |
sender emailSender |
| 33 |
writeBccHeader bool |
| 34 |
date string |
| 35 |
} |
| 36 |
|
| 37 |
// Email Date timestamp format |
| 38 |
const mailDateFormat = time.RFC1123Z |
| 39 |
|
| 40 |
// New returns an instance of MailYak using host as the SMTP server, and |
| 41 |
// authenticating with auth if non-nil. |
| 42 |
// |
| 43 |
// host must include the port number (i.e. "smtp.itsallbroken.com:25") |
| 44 |
// |
| 45 |
// mail := mailyak.New("smtp.itsallbroken.com:25", smtp.PlainAuth( |
| 46 |
// "", |
| 47 |
// "username", |
| 48 |
// "password", |
| 49 |
// "smtp.itsallbroken.com", |
| 50 |
// )) |
| 51 |
// |
| 52 |
// MailYak instances created with New will switch to using TLS after connecting |
| 53 |
// if the remote host supports the STARTTLS command. For an explicit TLS |
| 54 |
// connection, or to provide a custom tls.Config, use NewWithTLS() instead. |
| 55 |
func New(host string, auth smtp.Auth) *MailYak { |
| 56 |
return &MailYak{ |
| 57 |
headers: map[string][]string{}, |
| 58 |
host: host, |
| 59 |
auth: auth, |
| 60 |
sender: newSenderWithStartTLS(host), |
| 61 |
trimRegex: regexp.MustCompile("\r?\n"), |
| 62 |
writeBccHeader: false, |
| 63 |
date: time.Now().Format(mailDateFormat), |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// NewWithTLS returns an instance of MailYak using host as the SMTP server over |
| 68 |
// an explicit TLS connection, and authenticating with auth if non-nil. |
| 69 |
// |
| 70 |
// host must include the port number (i.e. "smtp.itsallbroken.com:25") |
| 71 |
// |
| 72 |
// mail := mailyak.NewWithTLS("smtp.itsallbroken.com:25", smtp.PlainAuth( |
| 73 |
// "", |
| 74 |
// "username", |
| 75 |
// "password", |
| 76 |
// "smtp.itsallbroken.com", |
| 77 |
// ), tlsConfig) |
| 78 |
// |
| 79 |
// If tlsConfig is nil, a sensible default is generated that can connect to |
| 80 |
// host. |
| 81 |
func NewWithTLS(host string, auth smtp.Auth, tlsConfig *tls.Config) (*MailYak, error) { |
| 82 |
// Construct a default MailYak instance |
| 83 |
m := New(host, auth) |
| 84 |
|
| 85 |
// Initialise the TLS sender with the (potentially nil) TLS config, swapping |
| 86 |
// it with the default STARTTLS sender. |
| 87 |
var err error |
| 88 |
m.sender, err = newSenderWithExplicitTLS(host, tlsConfig) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
|
| 93 |
return m, nil |
| 94 |
} |
| 95 |
|
| 96 |
// Send attempts to send the built email via the configured SMTP server. |
| 97 |
// |
| 98 |
// Attachments are read and the email timestamp is created when Send() is |
| 99 |
// called, and any connection/authentication errors will be returned by Send(). |
| 100 |
func (m *MailYak) Send() error { |
| 101 |
m.date = time.Now().Format(mailDateFormat) |
| 102 |
|
| 103 |
return m.sender.Send(m) |
| 104 |
} |
| 105 |
|
| 106 |
// MimeBuf returns the buffer containing all the RAW MIME data. |
| 107 |
// |
| 108 |
// MimeBuf is typically used with an API service such as Amazon SES that does |
| 109 |
// not use an SMTP interface. |
| 110 |
func (m *MailYak) MimeBuf() (*bytes.Buffer, error) { |
| 111 |
m.date = time.Now().Format(mailDateFormat) |
| 112 |
|
| 113 |
buf := &bytes.Buffer{} |
| 114 |
if err := m.buildMime(buf); err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
|
| 118 |
return buf, nil |
| 119 |
} |
| 120 |
|
| 121 |
// String returns a redacted description of the email state, typically for |
| 122 |
// logging or debugging purposes. |
| 123 |
// |
| 124 |
// Authentication information is not included in the returned string. |
| 125 |
func (m *MailYak) String() string { |
| 126 |
var ( |
| 127 |
att []string |
| 128 |
custom string |
| 129 |
) |
| 130 |
for _, a := range m.attachments { |
| 131 |
att = append(att, "{filename: "+a.filename+"}") |
| 132 |
} |
| 133 |
|
| 134 |
if len(m.headers) > 0 { |
| 135 |
var hdrs []string |
| 136 |
for k, v := range m.headers { |
| 137 |
hdrs = append(hdrs, fmt.Sprintf("%s: %q", k, v)) |
| 138 |
} |
| 139 |
custom = strings.Join(hdrs, ", ") + ", " |
| 140 |
} |
| 141 |
|
| 142 |
_, isTLSSender := m.sender.(*senderExplicitTLS) |
| 143 |
|
| 144 |
return fmt.Sprintf( |
| 145 |
"&MailYak{date: %q, from: %q, fromName: %q, html: %v bytes, plain: %v bytes, toAddrs: %v, "+ |
| 146 |
"bccAddrs: %v, subject: %q, %vhost: %q, attachments (%v): %v, auth set: %v, explicit tls: %v}", |
| 147 |
m.date, |
| 148 |
m.fromAddr, |
| 149 |
m.fromName, |
| 150 |
len(m.HTML().String()), |
| 151 |
len(m.Plain().String()), |
| 152 |
m.toAddrs, |
| 153 |
m.bccAddrs, |
| 154 |
m.subject, |
| 155 |
custom, |
| 156 |
m.host, |
| 157 |
len(att), |
| 158 |
att, |
| 159 |
m.auth != nil, |
| 160 |
isTLSSender, |
| 161 |
) |
| 162 |
} |
| 163 |
|
| 164 |
// HTML returns a BodyPart for the HTML email body. |
| 165 |
func (m *MailYak) HTML() *BodyPart { |
| 166 |
return &m.html |
| 167 |
} |
| 168 |
|
| 169 |
// Plain returns a BodyPart for the plain-text email body. |
| 170 |
func (m *MailYak) Plain() *BodyPart { |
| 171 |
return &m.plain |
| 172 |
} |
| 173 |
|
| 174 |
// getLocalName should return the sender domain to be used in the EHLO/HELO |
| 175 |
// command. |
| 176 |
func (m *MailYak) getLocalName() string { |
| 177 |
return m.localName |
| 178 |
} |
| 179 |
|
| 180 |
// getToAddrs should return a slice of email addresses to be added to the |
| 181 |
// RCPT TO command. |
| 182 |
func (m *MailYak) getToAddrs() []string { |
| 183 |
// Pre-allocate the slice to avoid growing it, we already know how big it |
| 184 |
// needs to be. |
| 185 |
addrs := len(m.toAddrs) + len(m.ccAddrs) + len(m.bccAddrs) |
| 186 |
out := make([]string, 0, addrs) |
| 187 |
|
| 188 |
out = append(out, stripNames(m.toAddrs)...) |
| 189 |
out = append(out, stripNames(m.ccAddrs)...) |
| 190 |
out = append(out, stripNames(m.bccAddrs)...) |
| 191 |
|
| 192 |
return out |
| 193 |
} |
| 194 |
|
| 195 |
// getFromAddr should return the address to be used in the MAIL FROM |
| 196 |
// command. |
| 197 |
func (m *MailYak) getFromAddr() string { |
| 198 |
return m.fromAddr |
| 199 |
} |
| 200 |
|
| 201 |
// getAuth should return the smtp.Auth if configured, nil if not. |
| 202 |
func (m *MailYak) getAuth() smtp.Auth { |
| 203 |
return m.auth |
| 204 |
} |
| 205 |
|
| 206 |
// stripNames returns a new slice with only the email parts from the RFC 5322 addresses. |
| 207 |
// |
| 208 |
// Or in other words, converts: |
| 209 |
// ["[email protected]", "John <[email protected]>", "invalid"] |
| 210 |
// to |
| 211 |
// ["[email protected]", "[email protected]", "invalid"]. |
| 212 |
// |
| 213 |
// Note that invalid addresses are kept as they are. |
| 214 |
func stripNames(addresses []string) []string { |
| 215 |
result := make([]string, 0, len(addresses)) |
| 216 |
|
| 217 |
for _, original := range addresses { |
| 218 |
addr, err := mail.ParseAddress(original) |
| 219 |
|
| 220 |
if err != nil { |
| 221 |
// add as it is |
| 222 |
result = append(result, original) |
| 223 |
} else { |
| 224 |
// add only the email part |
| 225 |
result = append(result, addr.Address) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return result |
| 230 |
} |