forked from go-mail/mail
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
233 lines (195 loc) · 5.3 KB
/
example_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package mail_test
import (
"fmt"
"html/template"
"io"
"log"
"time"
"gopkg.in/mail.v2"
)
func Example() {
m := mail.NewMessage()
m.SetHeader("From", "alex@example.com")
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
m.Attach("/home/Alex/lolcat.jpg")
d := mail.NewDialer("smtp.example.com", 587, "user", "123456")
d.StartTLSPolicy = mail.MandatoryStartTLS
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
// A daemon that listens to a channel and sends all incoming messages.
func Example_daemon() {
ch := make(chan *mail.Message)
go func() {
d := mail.NewDialer("smtp.example.com", 587, "user", "123456")
d.StartTLSPolicy = mail.MandatoryStartTLS
var s mail.SendCloser
var err error
open := false
for {
select {
case m, ok := <-ch:
if !ok {
return
}
if !open {
if s, err = d.Dial(); err != nil {
panic(err)
}
open = true
}
if err := mail.Send(s, m); err != nil {
log.Print(err)
}
// Close the connection to the SMTP server if no email was sent in
// the last 30 seconds.
case <-time.After(30 * time.Second):
if open {
if err := s.Close(); err != nil {
panic(err)
}
open = false
}
}
}
}()
// Use the channel in your program to send emails.
// Close the channel to stop the mail daemon.
close(ch)
}
// Efficiently send a customized newsletter to a list of recipients.
func Example_newsletter() {
// The list of recipients.
var list []struct {
Name string
Address string
}
d := mail.NewDialer("smtp.example.com", 587, "user", "123456")
d.StartTLSPolicy = mail.MandatoryStartTLS
s, err := d.Dial()
if err != nil {
panic(err)
}
m := mail.NewMessage()
for _, r := range list {
m.SetHeader("From", "no-reply@example.com")
m.SetAddressHeader("To", r.Address, r.Name)
m.SetHeader("Subject", "Newsletter #1")
m.SetBody("text/html", fmt.Sprintf("Hello %s!", r.Name))
if err := mail.Send(s, m); err != nil {
log.Printf("Could not send email to %q: %v", r.Address, err)
}
m.Reset()
}
}
// Send an email using a local SMTP server.
func Example_noAuth() {
m := mail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
d := mail.Dialer{Host: "localhost", Port: 587}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
// Send an email using an API or postfix.
func Example_noSMTP() {
m := mail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
s := mail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
// Implements you email-sending function, for example by calling
// an API, or running postfix, etc.
fmt.Println("From:", from)
fmt.Println("To:", to)
return nil
})
if err := mail.Send(s, m); err != nil {
panic(err)
}
// Output:
// From: from@example.com
// To: [to@example.com]
}
var m *mail.Message
func ExampleSetCopyFunc() {
m.Attach("foo.txt", mail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write([]byte("Content of foo.txt"))
return err
}))
}
func ExampleSetHeader() {
h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
m.Attach("foo.jpg", mail.SetHeader(h))
}
func ExampleRename() {
m.Attach("/tmp/0000146.jpg", mail.Rename("picture.jpg"))
}
func ExampleMessage_AddAlternative() {
m.SetBody("text/plain", "Hello!")
m.AddAlternative("text/html", "<p>Hello!</p>")
}
func ExampleMessage_AddAlternativeWriter() {
t := template.Must(template.New("example").Parse("Hello {{.}}!"))
m.AddAlternativeWriter("text/plain", func(w io.Writer) error {
return t.Execute(w, "Bob")
})
}
func ExampleMessage_Attach() {
m.Attach("/tmp/image.jpg")
}
func ExampleMessage_Embed() {
m.Embed("/tmp/image.jpg")
m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
}
func ExampleMessage_FormatAddress() {
m.SetHeader("To", m.FormatAddress("bob@example.com", "Bob"), m.FormatAddress("cora@example.com", "Cora"))
}
func ExampleMessage_FormatDate() {
m.SetHeaders(map[string][]string{
"X-Date": {m.FormatDate(time.Now())},
})
}
func ExampleMessage_SetAddressHeader() {
m.SetAddressHeader("To", "bob@example.com", "Bob")
}
func ExampleMessage_SetBody() {
m.SetBody("text/plain", "Hello!")
}
func ExampleMessage_SetBodyWriter() {
t := template.Must(template.New("example").Parse("Hello {{.}}!"))
m.SetBodyWriter("text/plain", func(w io.Writer) error {
return t.Execute(w, "Bob")
})
}
func ExampleMessage_SetDateHeader() {
m.SetDateHeader("X-Date", time.Now())
}
func ExampleMessage_SetHeader() {
m.SetHeader("Subject", "Hello!")
}
func ExampleMessage_SetHeaders() {
m.SetHeaders(map[string][]string{
"From": {m.FormatAddress("alex@example.com", "Alex")},
"To": {"bob@example.com", "cora@example.com"},
"Subject": {"Hello"},
})
}
func ExampleSetCharset() {
m = mail.NewMessage(mail.SetCharset("ISO-8859-1"))
}
func ExampleSetEncoding() {
m = mail.NewMessage(mail.SetEncoding(mail.Base64))
}
func ExampleSetPartEncoding() {
m.SetBody("text/plain", "Hello!", mail.SetPartEncoding(mail.Unencoded))
}