-
Notifications
You must be signed in to change notification settings - Fork 100
Builder Usage
James Hillyerd edited this page Feb 4, 2018
·
2 revisions
enmime provides a fluent builder to quickly create messages, its usage looks like this:
msg := enmime.Builder().
From("Alice", "alice@inbucket.org").
Subject("Greetings!").
Text([]byte("Hi Bob!")).
To("Bob", "bob@inbucket.org")
err := msg.Send(smtpHost, smtpAuth)
The builder methods return copies of the builder they are called on, instead of modifying the original. This allows the builder to be used like a template:
master := enmime.Builder().
From("Do Not Reply", "noreply@inbucket.org").
Subject("Inbucket Newsletter").
Text([]byte("Text body")).
HTML([]byte("<p>HTML body</p>"))
// master is immutable, causing each msg below to have a single recipient.
msg := master.To("Esteemed Customer", "user1@inbucket.org")
msg.Send(smtpHost, smtpAuth)
msg = master.To("Another Customer", "user2@inbucket.org")
msg.Send(smtpHost, smtpAuth)
Builder methods include:
AddAttachment(b []byte, contentType string, fileName string)
AddFileAttachment(path string)
AddFileInline(path string)
AddInline(b []byte, contentType string, fileName string, contentID string)
BCC(name, addr string)
BCCAddrs(bcc []mail.Address)
CC(name, addr string)
CCAddrs(cc []mail.Address)
Date(date time.Time)
From(name, addr string)
HTML(body []byte)
Header(name, value string)
ReplyTo(name, addr string)
Subject(subject string)
Text(body []byte)
To(name, addr string)
ToAddrs(to []mail.Address)
API docs can be found at https://godoc.org/github.com/jhillyerd/enmime#MailBuilder