forked from domodwyer/mailyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
88 lines (69 loc) · 2.34 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// This package provides a easy to use MIME email composer with support for
// attachments.
package mailyak
import (
"bytes"
"io"
"net/smtp"
"text/template"
)
func 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("[email protected]")
mail.From("[email protected]")
mail.FromName("Prince Anybody")
mail.Subject("Business proposition")
// Add a custom header
mail.AddHeader("X-TOTALLY-NOT-A-SCAM", "true")
// 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(" :( ")
}
}
func 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("[email protected]")
mail.From("[email protected]")
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(" :( ")
}
}
func ExampleBodyPart_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")
}
func ExampleBodyPart_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(" :( ")
}
}