forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
161 lines (137 loc) · 3.45 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
package mailjet
import (
"fmt"
"net/textproto"
"os"
"github.com/mailjet/mailjet-apiv3-go/resources"
)
func ExampleMailjetClient_List() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
var res []resources.Metadata
count, total, err := mj.List("metadata", &res)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("Count: %d\nTotal: %d\n", count, total)
fmt.Println("Resources:")
for _, resource := range res {
fmt.Println(resource.Name)
}
}
func ExampleMailjetClient_Get() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
var senders []resources.Sender
info := &Request{
Resource: "sender",
AltID: "[email protected]",
}
err := mj.Get(info, &senders)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if senders != nil {
fmt.Printf("Sender struct: %+v\n", senders[0])
}
}
func ExampleMailjetClient_Post() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
var senders []resources.Sender
fmr := &FullRequest{
Info: &Request{Resource: "sender"},
Payload: &resources.Sender{Name: "Default", Email: "[email protected]"},
}
err := mj.Post(fmr, &senders)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if senders != nil {
fmt.Printf("Data struct: %+v\n", senders[0])
}
}
func ExampleMailjetClient_Put() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
fmr := &FullRequest{
Info: &Request{Resource: "sender", AltID: "[email protected]"},
Payload: &resources.Sender{Name: "Bob", IsDefaultSender: true},
}
err := mj.Put(fmr, []string{"Name", "IsDefaultSender"})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else {
fmt.Println("Success")
}
}
func ExampleMailjetClient_Delete() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
info := &Request{
Resource: "sender",
AltID: "[email protected]",
}
err := mj.Delete(info)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else {
fmt.Println("Success")
}
}
func ExampleMailjetClient_SendMail() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
param := &InfoSendMail{
FromEmail: "[email protected]",
FromName: "Bob Patrick",
Recipients: []Recipient{
Recipient{
Email: "[email protected]",
},
},
Subject: "Hello World!",
TextPart: "Hi there !",
}
res, err := mj.SendMail(param)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Success")
fmt.Println(res)
}
}
func ExampleMailjetClient_SendMailSmtp() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := NewMailjetClient(publicKey, secretKey)
header := make(textproto.MIMEHeader)
header.Add("From", "[email protected]")
header.Add("To", "[email protected]")
header.Add("Subject", "Hello World!")
header.Add("X-Mailjet-Campaign", "test")
content := []byte("Hi there !")
info := &InfoSMTP{
From: "[email protected]",
Recipients: header["To"],
Header: header,
Content: content,
}
err := mj.SendMailSMTP(info)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Success")
}
}