-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathexamples_test.go
192 lines (163 loc) · 4.94 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
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
package mailgun_test
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
)
func ExampleEmailValidatorImpl_ValidateEmail() {
v := mailgun.NewEmailValidator("my_public_validation_api_key")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
ev, err := v.ValidateEmail(ctx, "[email protected]", false)
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 ExampleEmailValidatorImpl_ParseAddresses() {
v := mailgun.NewEmailValidator("my_public_validation_api_key")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
addressesThatParsed, unparsableAddresses, err := v.ParseAddresses(ctx, "Alice <[email protected]>", "[email protected]", "example.com")
if err != nil {
log.Fatal(err)
}
hittest := map[string]bool{
"Alice <[email protected]>": true,
"[email protected]": 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_UpdateMailingList() {
mg := mailgun.NewMailgun("example.com", "my_api_key")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_, err := mg.UpdateMailingList(ctx, "[email protected]", mailgun.MailingList{
Name: "Joe Stat",
Description: "Joe's status report list",
})
if err != nil {
log.Fatal(err)
}
}
func ExampleMailgunImpl_Send_constructed() {
mg := mailgun.NewMailgun("example.com", "my_api_key")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
m := mailgun.NewMessage(
"Excited User <[email protected]>",
"Hello World",
"Testing some Mailgun Awesomeness!",
)
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(ctx, 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 <[email protected]>
To: BARGLEGARF <[email protected]>
Content-Transfer-Encoding: 7bit
Date: Thu, 6 Mar 2014 00:37:52 +0000
Testing some Mailgun MIME awesomeness!
`
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
mg := mailgun.NewMailgun("example.com", "my_api_key")
m := mailgun.NewMIMEMessage(io.NopCloser(strings.NewReader(exampleMime)), "[email protected]")
_, id, err := mg.Send(ctx, m)
if err != nil {
log.Fatal(err)
}
log.Printf("Message id=%s", id)
}
func ExampleMailgunImpl_ListRoutes() {
mg := mailgun.NewMailgun("example.com", "my_api_key")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
it := mg.ListRoutes(nil)
var page []mailgun.Route
for it.Next(ctx, &page) {
for _, r := range page {
log.Printf("Route pri=%d expr=%s desc=%s", r.Priority, r.Expression, r.Description)
}
}
if it.Err() != nil {
log.Fatal(it.Err())
}
}
func ExampleMailgunImpl_VerifyWebhookSignature() {
// Create an instance of the Mailgun Client
mg, err := mailgun.NewMailgunFromEnv()
if err != nil {
fmt.Printf("mailgun error: %s\n", err)
os.Exit(1)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var payload mailgun.WebhookPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
fmt.Printf("decode JSON error: %s", err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
verified, err := mg.VerifyWebhookSignature(payload.Signature)
if err != nil {
fmt.Printf("verify error: %s\n", err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
if !verified {
w.WriteHeader(http.StatusNotAcceptable)
fmt.Printf("failed verification %+v\n", payload.Signature)
return
}
fmt.Printf("Verified Signature\n")
// Parse the raw event to extract the
e, err := mailgun.ParseEvent(payload.EventData)
if err != nil {
fmt.Printf("parse event error: %s\n", err)
return
}
switch event := e.(type) {
case *events.Accepted:
fmt.Printf("Accepted: auth: %t\n", event.Flags.IsAuthenticated)
case *events.Delivered:
fmt.Printf("Delivered transport: %s\n", event.Envelope.Transport)
}
})
fmt.Println("Running...")
if err := http.ListenAndServe(":9090", nil); err != nil {
fmt.Printf("serve error: %s\n", err)
os.Exit(1)
}
}