-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
examples.go
289 lines (239 loc) · 8.29 KB
/
examples.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Package main is examples using the go-mail package
*/
package main
import (
"context"
"log"
"os"
"strconv"
gomail "github.com/mrz1836/go-mail"
)
func main() {
// Run the AWS SES example
awsSesExample()
// Run the Mandrill example
// mandrillExample()
// Run the Postmark example
// postmarkExample()
// Run the SMTP example
// smtpExample()
// Example using ALL options available
// allOptionsExample()
}
// awsSesExample shows an example using AWS SES as the provider
func awsSesExample() {
// Config
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN")
if len(mail.FromDomain) == 0 {
log.Fatal("missing env: EMAIL_FROM_DOMAIN")
}
// Set the to field
toRecipients := os.Getenv("EMAIL_TEST_TO_RECIPIENT")
if len(toRecipients) == 0 {
log.Fatal("missing env: EMAIL_TEST_TO_RECIPIENT")
}
// Provider
mail.AwsSesAccessID = os.Getenv("EMAIL_AWS_SES_ACCESS_ID")
mail.AwsSesSecretKey = os.Getenv("EMAIL_AWS_SES_SECRET_KEY")
if len(mail.AwsSesAccessID) == 0 {
log.Fatal("missing env: EMAIL_AWS_SES_ACCESS_ID")
}
if len(mail.AwsSesSecretKey) == 0 {
log.Fatal("missing env: EMAIL_AWS_SES_SECRET_KEY")
}
provider := gomail.AwsSes
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}
// Create and send a basic email
email := mail.NewEmail()
email.HTMLContent = "<html><body>This is a <b>go-mail</b> example email using <i>HTML</i></body></html>"
email.Recipients = []string{toRecipients}
email.Subject = "example go-mail email using AWS SES"
// Send the email
if err = mail.SendEmail(context.Background(), email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
// mandrillExample shows an example using Mandrill as the provider
func mandrillExample() {
// Config
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN")
if len(mail.FromDomain) == 0 {
log.Fatal("missing env: EMAIL_FROM_DOMAIN")
}
// Set the to field
toRecipients := os.Getenv("EMAIL_TEST_TO_RECIPIENT")
if len(toRecipients) == 0 {
log.Fatal("missing env: EMAIL_TEST_TO_RECIPIENT")
}
// Provider
mail.MandrillAPIKey = os.Getenv("EMAIL_MANDRILL_KEY")
if len(mail.MandrillAPIKey) == 0 {
log.Fatal("missing env: EMAIL_MANDRILL_KEY")
}
provider := gomail.Mandrill
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}
// Create and send a basic email
email := mail.NewEmail()
email.HTMLContent = "<html><body>This is a <b>go-mail</b> example email using <i>HTML</i></body></html>"
email.Recipients = []string{toRecipients}
email.Subject = "example go-mail email using Mandrill"
// Send the email
if err = mail.SendEmail(context.Background(), email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
// postmarkExample shows an example using Postmark as the provider
func postmarkExample() {
// Config
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN")
if len(mail.FromDomain) == 0 {
log.Fatal("missing env: EMAIL_FROM_DOMAIN")
}
// Set the to field
toRecipients := os.Getenv("EMAIL_TEST_TO_RECIPIENT")
if len(toRecipients) == 0 {
log.Fatal("missing env: EMAIL_TEST_TO_RECIPIENT")
}
// Provider
mail.PostmarkServerToken = os.Getenv("EMAIL_POSTMARK_SERVER_TOKEN")
if len(mail.PostmarkServerToken) == 0 {
log.Fatal("missing env: EMAIL_POSTMARK_SERVER_TOKEN")
}
provider := gomail.Postmark
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}
// Create and send a basic email
email := mail.NewEmail()
email.HTMLContent = "<html><body>This is a <b>go-mail</b> example email using <i>HTML</i></body></html>"
email.Recipients = []string{toRecipients}
email.Subject = "example go-mail email using Postmark"
// Send the email
if err = mail.SendEmail(context.Background(), email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
// smtpExample shows an example using SMTP as the provider
func smtpExample() {
// Config
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN")
if len(mail.FromDomain) == 0 {
log.Fatal("missing env: EMAIL_FROM_DOMAIN")
}
// Set the to field
toRecipients := os.Getenv("EMAIL_TEST_TO_RECIPIENT")
if len(toRecipients) == 0 {
log.Fatal("missing env: EMAIL_TEST_TO_RECIPIENT")
}
// Provider
mail.SMTPHost = os.Getenv("EMAIL_SMTP_HOST")
mail.SMTPPort, _ = strconv.Atoi(os.Getenv("EMAIL_SMTP_PORT"))
mail.SMTPUsername = os.Getenv("EMAIL_SMTP_USERNAME")
mail.SMTPPassword = os.Getenv("EMAIL_SMTP_PASSWORD")
if len(mail.SMTPHost) == 0 {
log.Fatal("missing env: EMAIL_SMTP_HOST")
}
if len(mail.SMTPUsername) == 0 {
log.Fatal("missing env: EMAIL_SMTP_USERNAME")
}
if mail.SMTPPort == 0 {
log.Fatal("missing env: EMAIL_SMTP_PORT")
}
provider := gomail.SMTP
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}
// Create and send a basic email
email := mail.NewEmail()
email.HTMLContent = "<html><body>This is a <b>go-mail</b> example email using <i>HTML</i></body></html>"
email.Recipients = []string{toRecipients}
email.Subject = "example go-mail email using SMTP"
// Send the email
if err = mail.SendEmail(context.Background(), email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
log.Printf("email sent!")
}
// allOptionsExample is using the most amount of options/features
func allOptionsExample() {
// Define your service configuration
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN") // example.com
// Mandrill
mail.MandrillAPIKey = os.Getenv("EMAIL_MANDRILL_KEY") // aOfw3WU...
// AWS SES
mail.AwsSesAccessID = os.Getenv("EMAIL_AWS_SES_ACCESS_ID") // AKIAY...
mail.AwsSesSecretKey = os.Getenv("EMAIL_AWS_SES_SECRET_KEY") // tOpw3WU...
// Postmark
mail.PostmarkServerToken = os.Getenv("EMAIL_POSTMARK_SERVER_TOKEN") // AKIAY...
// SMTP
mail.SMTPHost = os.Getenv("EMAIL_SMTP_HOST") // example.com
mail.SMTPPort, _ = strconv.Atoi(os.Getenv("EMAIL_SMTP_PORT")) // 25
mail.SMTPUsername = os.Getenv("EMAIL_SMTP_USERNAME") // johndoe
mail.SMTPPassword = os.Getenv("EMAIL_SMTP_PASSWORD") // secretPassword
provider := gomail.SMTP // Other options: AwsSes Mandrill Postmark
// Start the service
err := mail.StartUp()
if err != nil {
log.Printf("error in StartUp: %s using provider: %x", err.Error(), provider)
}
// Available services given the config above
log.Printf("available service providers: %x", mail.AvailableProviders)
// Create a new email
email := mail.NewEmail()
email.PlainTextContent = "This is a go-mail example email using plain-text"
email.HTMLContent = "<html><body>This is a <b>go-mail</b> example email using <i>HTML</i></body></html>"
email.Recipients = []string{os.Getenv("EMAIL_TEST_TO_RECIPIENT")}
email.RecipientsCc = []string{os.Getenv("EMAIL_TEST_CC_RECIPIENT")}
email.RecipientsBcc = []string{os.Getenv("EMAIL_TEST_BCC_RECIPIENT")}
email.Subject = "testing go-mail - example email"
email.Tags = []string{"admin_alert"}
email.Important = true
email.TrackClicks = true
email.TrackOpens = true
email.AutoText = true
// Add an attachment
var f *os.File
f, err = os.Open("test-attachment-file.txt")
if err != nil {
log.Printf("unable to load file for attachment")
} else {
email.AddAttachment("test-attachment-file.txt", "text/plain", f)
}
// Send the email (basic example using one provider)
if err = mail.SendEmail(context.Background(), email, provider); err != nil {
log.Fatalf("error in SendEmail: %s using provider: %x", err.Error(), provider)
}
// Congrats!
log.Printf("all emails sent!")
}