Documentation ¶
Overview ¶
This library provides a simple interface for sending MIME compliant emails over SMTP.
Attachments are fully supported (attach anything that implements io.Reader) and the code is tried and tested in a production setting.
For convenience the HTML and Plain methods return a type implementing io.Writer, allowing email bodies to be composed directly from templating engines.
Example ¶
// Create a new email - specify the SMTP host and auth mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) mail.To("dom@itsallbroken.com") mail.From("nigerianprince@justneedshelp.com") mail.FromName("Prince Anybody") mail.Subject("Business proposition") // mail.HTMLWriter() and mail.PlainWriter() implement io.Writer, so you can // do handy things like parse a template directly into the email body - here // we just use io.WriteString() if _, err := io.WriteString(mail.HTML(), "So long, and thanks for all the fish."); err != nil { panic(" :( ") } // Or set the body using a string helper mail.Plain().Set("Get a real email client") // And you're done! if err := mail.Send(); err != nil { panic(" :( ") }
Output:
Example (Attachments) ¶
// This will be our attachment data buf := bytes.Buffer{} io.WriteString(&buf, "We're in the stickiest situation since Sticky the Stick Insect got stuck on a sticky bun.") // Create a new email - specify the SMTP host and auth mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) mail.To("dom@itsallbroken.com") mail.From("nigerianprince@justneedshelp.com") mail.HTML().Set("I am an email") // buf could be anything that implements io.Reader mail.Attach("sticky.txt", &buf) if err := mail.Send(); err != nil { panic(" :( ") }
Output:
Index ¶
- type AttachmentEmailer
- type BodyPart
- type Emailer
- type MailYak
- func (m *MailYak) Attach(name string, r io.Reader)
- func (m *MailYak) Bcc(addrs ...string)
- func (m *MailYak) ClearAttachments()
- func (m *MailYak) From(addr string)
- func (m *MailYak) FromName(name string)
- func (m *MailYak) HTML() *BodyPart
- func (m *MailYak) Plain() *BodyPart
- func (m *MailYak) ReplyTo(addr string)
- func (m MailYak) Send() error
- func (m *MailYak) Subject(sub string)
- func (m *MailYak) To(addrs ...string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AttachmentEmailer ¶
AttachmentEmailer extends the Emailer interface to provide attachment support
type BodyPart ¶
type BodyPart []byte
BodyPart is a byte slice that implements io.Writer
Example (String) ¶
// Create a new email - specify the SMTP host and auth mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) // Set the plain text email content using a string mail.Plain().Set("Get a real email client")
Output:
Example (Templates) ¶
// Create a new email mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) // Our pretend template data tmplData := struct { Language string }{"Go"} // Compile a template tmpl, err := template.New("html").Parse("I am an email template in {{ .Language }}") if err != nil { panic(" :( ") } // Execute the template directly into the email body if err := tmpl.Execute(mail.HTML(), tmplData); err != nil { panic(" :( ") }
Output:
type Emailer ¶
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 }
Emailer defines the interface implemented by MailYak
type MailYak ¶
type MailYak struct {
// contains filtered or unexported fields
}
func New ¶
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 (*MailYak) Attach ¶
Attach adds an attachment to the email with the given filename.
Note: The attachment data isn't read until Send() is called
func (*MailYak) Bcc ¶
Bcc sets a list of blind carbon copy (BCC) addresses
You can pass one or more addresses to this method, none of which are viewable to the recipients.
mail.Bcc("dom@itsallbroken.com", "another@itsallbroken.com")
or pass a slice of strings:
bccs := []string{ "one@itsallbroken.com", "two@itsallbroken.com" } mail.Bcc(bccs...)
func (*MailYak) ClearAttachments ¶
func (m *MailYak) ClearAttachments()
ClearAttachments removes all current attachments
func (MailYak) Send ¶
Send attempts to send the built email
Attachments are read when Send() is called, and any errors will be returned here.
func (*MailYak) To ¶
To sets a list of recipient addresses to send the email to
You can pass one or more addresses to this method, all of which are viewable to the recipients.
mail.To("dom@itsallbroken.com", "another@itsallbroken.com")
or pass a slice of strings:
tos := []string{ "one@itsallbroken.com", "two@itsallbroken.com" } mail.To(tos...)