| 1 |
package enmime |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io/ioutil" |
| 7 |
"mime" |
| 8 |
"net/mail" |
| 9 |
"net/textproto" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"reflect" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/jhillyerd/enmime/internal/stringutil" |
| 16 |
) |
| 17 |
|
| 18 |
// MailBuilder facilitates the easy construction of a MIME message. Each manipulation method |
| 19 |
// returns a copy of the receiver struct. It can be considered immutable if the caller does not |
| 20 |
// modify the string and byte slices passed in. Immutability allows the headers or entire message |
| 21 |
// to be reused across multiple threads. |
| 22 |
type MailBuilder struct { |
| 23 |
to, cc, bcc []mail.Address |
| 24 |
from mail.Address |
| 25 |
replyTo mail.Address |
| 26 |
subject string |
| 27 |
date time.Time |
| 28 |
header textproto.MIMEHeader |
| 29 |
text, html []byte |
| 30 |
inlines, attachments []*Part |
| 31 |
err error |
| 32 |
} |
| 33 |
|
| 34 |
// Builder returns an empty MailBuilder struct. |
| 35 |
func Builder() MailBuilder { |
| 36 |
return MailBuilder{} |
| 37 |
} |
| 38 |
|
| 39 |
// Error returns the stored error from a file attachment/inline read or nil. |
| 40 |
func (p MailBuilder) Error() error { |
| 41 |
return p.err |
| 42 |
} |
| 43 |
|
| 44 |
// Date returns a copy of MailBuilder with the specified Date header. |
| 45 |
func (p MailBuilder) Date(date time.Time) MailBuilder { |
| 46 |
p.date = date |
| 47 |
return p |
| 48 |
} |
| 49 |
|
| 50 |
// From returns a copy of MailBuilder with the specified From header. |
| 51 |
func (p MailBuilder) From(name, addr string) MailBuilder { |
| 52 |
p.from = mail.Address{Name: name, Address: addr} |
| 53 |
return p |
| 54 |
} |
| 55 |
|
| 56 |
// Subject returns a copy of MailBuilder with the specified Subject header. |
| 57 |
func (p MailBuilder) Subject(subject string) MailBuilder { |
| 58 |
p.subject = subject |
| 59 |
return p |
| 60 |
} |
| 61 |
|
| 62 |
// To returns a copy of MailBuilder with this name & address appended to the To header. name may be |
| 63 |
// empty. |
| 64 |
func (p MailBuilder) To(name, addr string) MailBuilder { |
| 65 |
if len(addr) > 0 { |
| 66 |
p.to = append(p.to, mail.Address{Name: name, Address: addr}) |
| 67 |
} |
| 68 |
return p |
| 69 |
} |
| 70 |
|
| 71 |
// ToAddrs returns a copy of MailBuilder with the specified To addresses. |
| 72 |
func (p MailBuilder) ToAddrs(to []mail.Address) MailBuilder { |
| 73 |
p.to = to |
| 74 |
return p |
| 75 |
} |
| 76 |
|
| 77 |
// CC returns a copy of MailBuilder with this name & address appended to the CC header. name may be |
| 78 |
// empty. |
| 79 |
func (p MailBuilder) CC(name, addr string) MailBuilder { |
| 80 |
if len(addr) > 0 { |
| 81 |
p.cc = append(p.cc, mail.Address{Name: name, Address: addr}) |
| 82 |
} |
| 83 |
return p |
| 84 |
} |
| 85 |
|
| 86 |
// CCAddrs returns a copy of MailBuilder with the specified CC addresses. |
| 87 |
func (p MailBuilder) CCAddrs(cc []mail.Address) MailBuilder { |
| 88 |
p.cc = cc |
| 89 |
return p |
| 90 |
} |
| 91 |
|
| 92 |
// BCC returns a copy of MailBuilder with this name & address appended to the BCC list. name may be |
| 93 |
// empty. This method only has an effect if the Send method is used to transmit the message, there |
| 94 |
// is no effect on the parts returned by Build(). |
| 95 |
func (p MailBuilder) BCC(name, addr string) MailBuilder { |
| 96 |
if len(addr) > 0 { |
| 97 |
p.bcc = append(p.bcc, mail.Address{Name: name, Address: addr}) |
| 98 |
} |
| 99 |
return p |
| 100 |
} |
| 101 |
|
| 102 |
// BCCAddrs returns a copy of MailBuilder with the specified as the blind CC list. This method only |
| 103 |
// has an effect if the Send method is used to transmit the message, there is no effect on the parts |
| 104 |
// returned by Build(). |
| 105 |
func (p MailBuilder) BCCAddrs(bcc []mail.Address) MailBuilder { |
| 106 |
p.bcc = bcc |
| 107 |
return p |
| 108 |
} |
| 109 |
|
| 110 |
// ReplyTo returns a copy of MailBuilder with this name & address appended to the To header. name |
| 111 |
// may be empty. |
| 112 |
func (p MailBuilder) ReplyTo(name, addr string) MailBuilder { |
| 113 |
p.replyTo = mail.Address{Name: name, Address: addr} |
| 114 |
return p |
| 115 |
} |
| 116 |
|
| 117 |
// Header returns a copy of MailBuilder with the specified value added to the named header. |
| 118 |
func (p MailBuilder) Header(name, value string) MailBuilder { |
| 119 |
// Copy existing header map |
| 120 |
h := textproto.MIMEHeader{} |
| 121 |
for k, v := range p.header { |
| 122 |
h[k] = v |
| 123 |
} |
| 124 |
h.Add(name, value) |
| 125 |
p.header = h |
| 126 |
return p |
| 127 |
} |
| 128 |
|
| 129 |
// Text returns a copy of MailBuilder that will use the provided bytes for its text/plain Part. |
| 130 |
func (p MailBuilder) Text(body []byte) MailBuilder { |
| 131 |
p.text = body |
| 132 |
return p |
| 133 |
} |
| 134 |
|
| 135 |
// HTML returns a copy of MailBuilder that will use the provided bytes for its text/html Part. |
| 136 |
func (p MailBuilder) HTML(body []byte) MailBuilder { |
| 137 |
p.html = body |
| 138 |
return p |
| 139 |
} |
| 140 |
|
| 141 |
// AddAttachment returns a copy of MailBuilder that includes the specified attachment. |
| 142 |
func (p MailBuilder) AddAttachment(b []byte, contentType string, fileName string) MailBuilder { |
| 143 |
part := NewPart(contentType) |
| 144 |
part.Content = b |
| 145 |
part.FileName = fileName |
| 146 |
part.Disposition = cdAttachment |
| 147 |
p.attachments = append(p.attachments, part) |
| 148 |
return p |
| 149 |
} |
| 150 |
|
| 151 |
// AddFileAttachment returns a copy of MailBuilder that includes the specified attachment. |
| 152 |
// fileName, will be populated from the base name of path. Content type will be detected from the |
| 153 |
// path extension. |
| 154 |
func (p MailBuilder) AddFileAttachment(path string) MailBuilder { |
| 155 |
// Only allow first p.err value |
| 156 |
if p.err != nil { |
| 157 |
return p |
| 158 |
} |
| 159 |
f, err := os.Open(path) |
| 160 |
if err != nil { |
| 161 |
p.err = err |
| 162 |
return p |
| 163 |
} |
| 164 |
b, err := ioutil.ReadAll(f) |
| 165 |
if err != nil { |
| 166 |
p.err = err |
| 167 |
return p |
| 168 |
} |
| 169 |
name := filepath.Base(path) |
| 170 |
ctype := mime.TypeByExtension(filepath.Ext(name)) |
| 171 |
return p.AddAttachment(b, ctype, name) |
| 172 |
} |
| 173 |
|
| 174 |
// AddInline returns a copy of MailBuilder that includes the specified inline. fileName and |
| 175 |
// contentID may be left empty. |
| 176 |
func (p MailBuilder) AddInline( |
| 177 |
b []byte, |
| 178 |
contentType string, |
| 179 |
fileName string, |
| 180 |
contentID string, |
| 181 |
) MailBuilder { |
| 182 |
part := NewPart(contentType) |
| 183 |
part.Content = b |
| 184 |
part.FileName = fileName |
| 185 |
part.Disposition = cdInline |
| 186 |
part.ContentID = contentID |
| 187 |
p.inlines = append(p.inlines, part) |
| 188 |
return p |
| 189 |
} |
| 190 |
|
| 191 |
// AddFileInline returns a copy of MailBuilder that includes the specified inline. fileName and |
| 192 |
// contentID will be populated from the base name of path. Content type will be detected from the |
| 193 |
// path extension. |
| 194 |
func (p MailBuilder) AddFileInline(path string) MailBuilder { |
| 195 |
// Only allow first p.err value |
| 196 |
if p.err != nil { |
| 197 |
return p |
| 198 |
} |
| 199 |
f, err := os.Open(path) |
| 200 |
if err != nil { |
| 201 |
p.err = err |
| 202 |
return p |
| 203 |
} |
| 204 |
b, err := ioutil.ReadAll(f) |
| 205 |
if err != nil { |
| 206 |
p.err = err |
| 207 |
return p |
| 208 |
} |
| 209 |
name := filepath.Base(path) |
| 210 |
ctype := mime.TypeByExtension(filepath.Ext(name)) |
| 211 |
return p.AddInline(b, ctype, name, name) |
| 212 |
} |
| 213 |
|
| 214 |
// Build performs some basic validations, then constructs a tree of Part structs from the configured |
| 215 |
// MailBuilder. It will set the Date header to now if it was not explicitly set. |
| 216 |
func (p MailBuilder) Build() (*Part, error) { |
| 217 |
if p.err != nil { |
| 218 |
return nil, p.err |
| 219 |
} |
| 220 |
// Validations |
| 221 |
if p.from.Address == "" { |
| 222 |
return nil, errors.New("from not set") |
| 223 |
} |
| 224 |
if len(p.to)+len(p.cc)+len(p.bcc) == 0 { |
| 225 |
return nil, errors.New(ErrorMissingRecipient) |
| 226 |
} |
| 227 |
// Fully loaded structure; the presence of text, html, inlines, and attachments will determine |
| 228 |
// how much is necessary: |
| 229 |
// |
| 230 |
// multipart/mixed |
| 231 |
// |- multipart/related |
| 232 |
// | |- multipart/alternative |
| 233 |
// | | |- text/plain |
| 234 |
// | | `- text/html |
| 235 |
// | `- inlines.. |
| 236 |
// `- attachments.. |
| 237 |
// |
| 238 |
// We build this tree starting at the leaves, re-rooting as needed. |
| 239 |
var root, part *Part |
| 240 |
if p.text != nil || p.html == nil { |
| 241 |
root = NewPart(ctTextPlain) |
| 242 |
root.Content = p.text |
| 243 |
root.Charset = utf8 |
| 244 |
} |
| 245 |
if p.html != nil { |
| 246 |
part = NewPart(ctTextHTML) |
| 247 |
part.Content = p.html |
| 248 |
part.Charset = utf8 |
| 249 |
if root == nil { |
| 250 |
root = part |
| 251 |
} else { |
| 252 |
root.NextSibling = part |
| 253 |
} |
| 254 |
} |
| 255 |
if p.text != nil && p.html != nil { |
| 256 |
// Wrap Text & HTML bodies |
| 257 |
part = root |
| 258 |
root = NewPart(ctMultipartAltern) |
| 259 |
root.AddChild(part) |
| 260 |
} |
| 261 |
if len(p.inlines) > 0 { |
| 262 |
part = root |
| 263 |
root = NewPart(ctMultipartRelated) |
| 264 |
root.AddChild(part) |
| 265 |
for _, ip := range p.inlines { |
| 266 |
// Copy inline Part to isolate mutations |
| 267 |
part = &Part{} |
| 268 |
*part = *ip |
| 269 |
part.Header = make(textproto.MIMEHeader) |
| 270 |
root.AddChild(part) |
| 271 |
} |
| 272 |
} |
| 273 |
if len(p.attachments) > 0 { |
| 274 |
part = root |
| 275 |
root = NewPart(ctMultipartMixed) |
| 276 |
root.AddChild(part) |
| 277 |
for _, ap := range p.attachments { |
| 278 |
// Copy attachment Part to isolate mutations |
| 279 |
part = &Part{} |
| 280 |
*part = *ap |
| 281 |
part.Header = make(textproto.MIMEHeader) |
| 282 |
root.AddChild(part) |
| 283 |
} |
| 284 |
} |
| 285 |
// Headers |
| 286 |
h := root.Header |
| 287 |
h.Set(hnMIMEVersion, "1.0") |
| 288 |
h.Set("From", p.from.String()) |
| 289 |
h.Set("Subject", p.subject) |
| 290 |
if len(p.to) > 0 { |
| 291 |
h.Set("To", stringutil.JoinAddress(p.to)) |
| 292 |
} |
| 293 |
if len(p.cc) > 0 { |
| 294 |
h.Set("Cc", stringutil.JoinAddress(p.cc)) |
| 295 |
} |
| 296 |
if p.replyTo.Address != "" { |
| 297 |
h.Set("Reply-To", p.replyTo.String()) |
| 298 |
} |
| 299 |
date := p.date |
| 300 |
if date.IsZero() { |
| 301 |
date = time.Now() |
| 302 |
} |
| 303 |
h.Set("Date", date.Format(time.RFC1123Z)) |
| 304 |
for k, v := range p.header { |
| 305 |
for _, s := range v { |
| 306 |
h.Add(k, s) |
| 307 |
} |
| 308 |
} |
| 309 |
return root, nil |
| 310 |
} |
| 311 |
|
| 312 |
// SendWithReversePath encodes the message and sends it via the specified Sender. |
| 313 |
func (p MailBuilder) SendWithReversePath(sender Sender, from string) error { |
| 314 |
buf := &bytes.Buffer{} |
| 315 |
root, err := p.Build() |
| 316 |
if err != nil { |
| 317 |
return err |
| 318 |
} |
| 319 |
err = root.Encode(buf) |
| 320 |
if err != nil { |
| 321 |
return err |
| 322 |
} |
| 323 |
recips := make([]string, 0, len(p.to)+len(p.cc)+len(p.bcc)) |
| 324 |
for _, a := range p.to { |
| 325 |
recips = append(recips, a.Address) |
| 326 |
} |
| 327 |
for _, a := range p.cc { |
| 328 |
recips = append(recips, a.Address) |
| 329 |
} |
| 330 |
for _, a := range p.bcc { |
| 331 |
recips = append(recips, a.Address) |
| 332 |
} |
| 333 |
return sender.Send(from, recips, buf.Bytes()) |
| 334 |
} |
| 335 |
|
| 336 |
// Send encodes the message and sends it via the specified Sender, using the address provided to |
| 337 |
// `From()` as the reverse-path. |
| 338 |
func (p MailBuilder) Send(sender Sender) error { |
| 339 |
return p.SendWithReversePath(sender, p.from.Address) |
| 340 |
} |
| 341 |
|
| 342 |
// Equals uses the reflect package to test two MailBuilder structs for equality, primarily for unit |
| 343 |
// tests. |
| 344 |
func (p MailBuilder) Equals(o MailBuilder) bool { |
| 345 |
return reflect.DeepEqual(p, o) |
| 346 |
} |