| 1 |
// This package provides a easy to use MIME email composer with support for |
| 2 |
// attachments. |
| 3 |
package mailyak |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"crypto/tls" |
| 8 |
"io" |
| 9 |
"net/smtp" |
| 10 |
"text/template" |
| 11 |
) |
| 12 |
|
| 13 |
func Example() { |
| 14 |
// Create a new email - specify the SMTP host:port and auth (or nil if not |
| 15 |
// needed). |
| 16 |
// |
| 17 |
// If you want to connect using TLS, use NewWithTLS() instead. |
| 18 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 19 |
|
| 20 |
mail.To("[email protected]") |
| 21 |
mail.From("[email protected]") |
| 22 |
mail.FromName("Prince Anybody") |
| 23 |
|
| 24 |
mail.Subject("Business proposition") |
| 25 |
|
| 26 |
// Add a custom header |
| 27 |
mail.AddHeader("X-TOTALLY-NOT-A-SCAM", "true") |
| 28 |
|
| 29 |
// mail.HTMLWriter() and mail.PlainWriter() implement io.Writer, so you can |
| 30 |
// do handy things like parse a template directly into the email body - here |
| 31 |
// we just use io.WriteString() |
| 32 |
if _, err := io.WriteString(mail.HTML(), "So long, and thanks for all the fish."); err != nil { |
| 33 |
panic(" :( ") |
| 34 |
} |
| 35 |
|
| 36 |
// Or set the body using a string helper |
| 37 |
mail.Plain().Set("Get a real email client") |
| 38 |
|
| 39 |
// And you're done! |
| 40 |
if err := mail.Send(); err != nil { |
| 41 |
panic(" :( ") |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func Example_attachments() { |
| 46 |
// This will be our attachment data |
| 47 |
buf := &bytes.Buffer{} |
| 48 |
_, _ = io.WriteString(buf, "We're in the stickiest situation since Sticky the Stick Insect got stuck on a sticky bun.") |
| 49 |
|
| 50 |
// Create a new email - specify the SMTP host:port and auth (or nil if not |
| 51 |
// needed). |
| 52 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 53 |
|
| 54 |
mail.To("[email protected]") |
| 55 |
mail.From("[email protected]") |
| 56 |
mail.HTML().Set("I am an email") |
| 57 |
|
| 58 |
// buf could be anything that implements io.Reader, like a file on disk or |
| 59 |
// an in-memory buffer. |
| 60 |
mail.Attach("sticky.txt", buf) |
| 61 |
|
| 62 |
if err := mail.Send(); err != nil { |
| 63 |
panic(" :( ") |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func ExampleBodyPart_string() { |
| 68 |
// Create a new email - specify the SMTP host and auth |
| 69 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 70 |
|
| 71 |
// Set the plain text email content using a string |
| 72 |
mail.Plain().Set("Get a real email client") |
| 73 |
} |
| 74 |
|
| 75 |
func ExampleNewWithTLS() { |
| 76 |
// Create a new MailYak instance that uses an explicit TLS connection. This |
| 77 |
// ensures no communication is performed in plain-text. |
| 78 |
// |
| 79 |
// Specify the SMTP host:port to connect to, the authentication credentials |
| 80 |
// (or nil if not needed), and use an automatically generated TLS |
| 81 |
// configuration by passing nil as the tls.Config argument. |
| 82 |
mail, err := NewWithTLS("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"), nil) |
| 83 |
if err != nil { |
| 84 |
panic("failed to initialise a TLS instance :(") |
| 85 |
} |
| 86 |
|
| 87 |
mail.Plain().Set("Have some encrypted goodness") |
| 88 |
if err := mail.Send(); err != nil { |
| 89 |
panic(" :( ") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func ExampleNewWithTLS_with_config() { |
| 94 |
// Create a new MailYak instance that uses an explicit TLS connection. This |
| 95 |
// ensures no communication is performed in plain-text. |
| 96 |
// |
| 97 |
// Specify the SMTP host:port to connect to, the authentication credentials |
| 98 |
// (or nil if not needed), and use the tls.Config provided. |
| 99 |
mail, err := NewWithTLS( |
| 100 |
"mail.host.com:25", |
| 101 |
smtp.PlainAuth("", "user", "pass", "mail.host.com"), |
| 102 |
&tls.Config{ |
| 103 |
// ServerName is used to verify the hostname on the returned |
| 104 |
// certificates unless InsecureSkipVerify is given. It is also included |
| 105 |
// in the client's handshake to support virtual hosting unless it is |
| 106 |
// an IP address. |
| 107 |
ServerName: "mail.host.com", |
| 108 |
|
| 109 |
// Negotiate a connection that uses at least TLS v1.2, or refuse the |
| 110 |
// connection if the server does not support it. Most do, and it is |
| 111 |
// a very good idea to enforce it! |
| 112 |
MinVersion: tls.VersionTLS12, |
| 113 |
}, |
| 114 |
) |
| 115 |
if err != nil { |
| 116 |
panic("failed to initialise a TLS instance :(") |
| 117 |
} |
| 118 |
|
| 119 |
mail.Plain().Set("Have some encrypted goodness") |
| 120 |
if err := mail.Send(); err != nil { |
| 121 |
panic(" :( ") |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func ExampleBodyPart_templates() { |
| 126 |
// Create a new email |
| 127 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 128 |
|
| 129 |
// Our pretend template data |
| 130 |
tmplData := struct { |
| 131 |
Language string |
| 132 |
}{"Go"} |
| 133 |
|
| 134 |
// Compile a template |
| 135 |
tmpl, err := template.New("html").Parse("I am an email template in {{ .Language }}") |
| 136 |
if err != nil { |
| 137 |
panic(" :( ") |
| 138 |
} |
| 139 |
|
| 140 |
// Execute the template directly into the email body |
| 141 |
if err := tmpl.Execute(mail.HTML(), tmplData); err != nil { |
| 142 |
panic(" :( ") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func ExampleMailYak_AttachInline() { |
| 147 |
// Create a new email |
| 148 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 149 |
mail.To("[email protected]") |
| 150 |
mail.From("[email protected]") |
| 151 |
|
| 152 |
// Initialise an io.Reader that contains your image (typically read from |
| 153 |
// disk, or embedded in memory). |
| 154 |
// |
| 155 |
// Here we use an empty buffer as a mock. |
| 156 |
imageBuffer := &bytes.Buffer{} |
| 157 |
|
| 158 |
// Add the image as an attachment. |
| 159 |
// |
| 160 |
// To reference it, use the name as the cid value. |
| 161 |
mail.AttachInline("myimage", imageBuffer) |
| 162 |
|
| 163 |
// Set the HTML body, which includes the inline CID reference. |
| 164 |
mail.HTML().Set(` |
| 165 |
<html> |
| 166 |
<body> |
| 167 |
<img src="cid:myimage"/> |
| 168 |
</body> |
| 169 |
</html> |
| 170 |
`) |
| 171 |
|
| 172 |
// Send it! |
| 173 |
if err := mail.Send(); err != nil { |
| 174 |
panic(" :( ") |
| 175 |
} |
| 176 |
} |