forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
117 lines (108 loc) · 3.06 KB
/
examples_test.go
File metadata and controls
117 lines (108 loc) · 3.06 KB
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
package mailgun
import (
"io/ioutil"
"log"
"strings"
"time"
)
func ExampleMailgunImpl_ValidateEmail() {
mg := NewMailgun("example.com", "", "my_public_api_key")
ev, err := mg.ValidateEmail("joe@example.com")
if err != nil {
log.Fatal(err)
}
if !ev.IsValid {
log.Fatal("Expected valid e-mail address")
}
log.Printf("Parts local_part=%s domain=%s display_name=%s", ev.Parts.LocalPart, ev.Parts.Domain, ev.Parts.DisplayName)
if ev.DidYouMean != "" {
log.Printf("The address is syntactically valid, but perhaps has a typo.")
log.Printf("Did you mean %s instead?", ev.DidYouMean)
}
}
func ExampleMailgunImpl_ParseAddresses() {
mg := NewMailgun("example.com", "", "my_public_api_key")
addressesThatParsed, unparsableAddresses, err := mg.ParseAddresses("Alice <alice@example.com>", "bob@example.com", "example.com")
if err != nil {
log.Fatal(err)
}
hittest := map[string]bool{
"Alice <alice@example.com>": true,
"bob@example.com": true,
}
for _, a := range addressesThatParsed {
if !hittest[a] {
log.Fatalf("Expected %s to be parsable", a)
}
}
if len(unparsableAddresses) != 1 {
log.Fatalf("Expected 1 address to be unparsable; got %d", len(unparsableAddresses))
}
}
func ExampleMailgunImpl_UpdateList() {
mg := NewMailgun("example.com", "my_api_key", "")
_, err := mg.UpdateList("joe-stat@example.com", List{
Name: "Joe Stat",
Description: "Joe's status report list",
})
if err != nil {
log.Fatal(err)
}
}
func ExampleMailgunImpl_Send_constructed() {
mg := NewMailgun("example.com", "my_api_key", "")
m := NewMessage(
"Excited User <me@example.com>",
"Hello World",
"Testing some Mailgun Awesomeness!",
"baz@example.com",
"bar@example.com",
)
m.SetTracking(true)
m.SetDeliveryTime(time.Now().Add(24 * time.Hour))
m.SetHtml("<html><body><h1>Testing some Mailgun Awesomeness!!</h1></body></html>")
_, id, err := mg.Send(m)
if err != nil {
log.Fatal(err)
}
log.Printf("Message id=%s", id)
}
func ExampleMailgunImpl_Send_mime() {
exampleMime := `Content-Type: text/plain; charset="ascii"
Subject: Joe's Example Subject
From: Joe Example <joe@example.com>
To: BARGLEGARF <bargle.garf@example.com>
Content-Transfer-Encoding: 7bit
Date: Thu, 6 Mar 2014 00:37:52 +0000
Testing some Mailgun MIME awesomeness!
`
mg := NewMailgun("example.com", "my_api_key", "")
m := NewMIMEMessage(ioutil.NopCloser(strings.NewReader(exampleMime)), "bargle.garf@example.com")
_, id, err := mg.Send(m)
if err != nil {
log.Fatal(err)
}
log.Printf("Message id=%s", id)
}
func ExampleMailgunImpl_GetRoutes() {
mg := NewMailgun("example.com", "my_api_key", "")
n, routes, err := mg.GetRoutes(DefaultLimit, DefaultSkip)
if err != nil {
log.Fatal(err)
}
if n > len(routes) {
log.Printf("More routes exist than has been returned.")
}
for _, r := range routes {
log.Printf("Route pri=%d expr=%s desc=%s", r.Priority, r.Expression, r.Description)
}
}
func ExampleMailgunImpl_UpdateRoute() {
mg := NewMailgun("example.com", "my_api_key", "")
_, err := mg.UpdateRoute("route-id-here", Route{
Priority: 2,
})
if err != nil {
log.Fatal(err)
}
}