-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathexample_test.go
161 lines (138 loc) · 3.53 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_test
import (
"fmt"
"net/textproto"
"os"
"github.com/mailjet/mailjet-apiv3-go/v4"
"github.com/mailjet/mailjet-apiv3-go/v4/resources"
)
var (
publicKey = os.Getenv("MJ_APIKEY_PUBLIC")
privateKey = os.Getenv("MJ_APIKEY_PRIVATE")
)
func exampleMailjetClientList() {
mj := mailjet.NewMailjetClient(publicKey, privateKey)
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 exampleMailjetClientGet() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := mailjet.NewMailjetClient(publicKey, secretKey)
var senders []resources.Sender
info := &mailjet.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 exampleMailjetClientPost() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := mailjet.NewMailjetClient(publicKey, secretKey)
var senders []resources.Sender
fmr := &mailjet.FullRequest{
Info: &mailjet.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 exampleMailjetClientPut() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := mailjet.NewMailjetClient(publicKey, secretKey)
fmr := &mailjet.FullRequest{
Info: &mailjet.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 exampleMailjetClientDelete() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := mailjet.NewMailjetClient(publicKey, secretKey)
info := &mailjet.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 exampleMailjetClientSendMail() {
publicKey := os.Getenv("MJ_APIKEY_PUBLIC")
secretKey := os.Getenv("MJ_APIKEY_PRIVATE")
mj := mailjet.NewMailjetClient(publicKey, secretKey)
param := &mailjet.InfoSendMail{
FromEmail: "[email protected]",
FromName: "Bob Patrick",
Recipients: []mailjet.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 exampleMailjetClientSendMailSMTP() {
mj := mailjet.NewMailjetClient(publicKey, privateKey)
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 := &mailjet.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")
}
}