-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSmartEmailing.php
More file actions
281 lines (238 loc) · 6.26 KB
/
Copy pathSmartEmailing.php
File metadata and controls
281 lines (238 loc) · 6.26 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
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
<?php
namespace ADT;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Nette\Utils\Json;
/**
* SmartEmailing API v3
* http://docs.smartemailing.apiary.io/
*/
class SmartEmailing
{
const NODE_PING = 'ping';
const NODE_CHECK_CREDENTIALS = 'check-credentials';
const NODE_CONTACTLISTS = 'contactlists';
const NODE_CUSTOMFIELDS = 'customfields';
const NODE_CUSTOMFIELDS_OPTIONS = 'customfield-options';
const NODE_CONTACT_CUSTOMFIELDS = 'contact-customfields';
const NODE_CHANGE_EMAILADDRESS = 'change-emailaddress';
const NODE_CONTACT_FORGET = 'contacts/forget';
const NODE_CONTACTS = 'contacts';
const NODE_IMPORT = 'import';
const NODE_PURPOSES = 'purposes';
const NODE_PURPOSE_CONNECTIONS = 'purpose-connections';
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
protected $url = 'https://app.smartemailing.cz/api/v3';
protected $username;
protected $token;
/**
* SmartEmailing constructor.
*
* @param string $username
* @param string $token
*/
public function __construct($username, $token)
{
$this->username = $username;
$this->token = $token;
}
/**
* Aliveness test
*
* @return array
* @throws SmartEmailingException
*/
public function ping()
{
return $this->call(self::METHOD_GET, self::NODE_PING);
}
/**
* Login test
*
* @return array
* @throws SmartEmailingException
*/
public function checkCredentials()
{
return $this->call(self::METHOD_GET, self::NODE_CHECK_CREDENTIALS);
}
/**
* @param string $name
* @param string $senderName
* @param string $senderEmail email
* @param string $replyTo email
* @param string|null $publicName
* @throws SmartEmailingException
*/
public function createContactlist($name, $senderName, $senderEmail, $replyTo, $publicName = NULL)
{
$data = [
'name' => $name,
'sendername' => $senderName,
'senderemail' => $senderEmail,
'replyto' => $replyTo,
];
if (is_string($publicName)) {
$data['publicname'] = $publicName;
}
return $this->call(self::METHOD_POST, self::NODE_CONTACTLISTS, $data);
}
/**
* @return array
* @throws SmartEmailingException
*/
public function getContactlists()
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS);
}
/**
* @param $id
* @return array
* @throws SmartEmailingException
*/
public function getContactlist($id)
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS . "/" . $id);
}
/**
* @param $id
* @param string|null $name
* @param string|null $senderName
* @param string|null $senderEmail
* @param string|null $replyTo
* @param string|null $publicName
* @return array
* @throws SmartEmailingException
*/
public function updateContactlist($id, $name = NULL, $senderName = NULL, $senderEmail = NULL, $replyTo = NULL, $publicName = NULL)
{
$data = [];
if ($name !== NULL) $data['name'] = $name;
if ($senderName !== NULL) $data['sendername'] = $senderName;
if ($senderEmail !== NULL) $data['senderemail'] = $senderEmail;
if ($replyTo !== NULL) $data['replyto'] = $replyTo;
if ($publicName !== NULL) $data['publicname'] = $publicName;
if ($publicName !== NULL) $data['publicname'] = $publicName;
return $this->call(self::METHOD_PUT, self::NODE_CONTACTLISTS . "/" . $id, $data);
}
/**
* @param string $from
* @param string $to
* @return array
* @throws SmartEmailingException
*/
public function changeContactEmail($from, $to)
{
return $this->call(self::METHOD_POST, self::NODE_CHANGE_EMAILADDRESS, [
'from' => $from,
'to' => $to,
]);
}
/**
* Deletes contact and anonymizes all his leftover data. This action cannot be undone.
* This is GDPR complaint method to secure contact's right to be forgotten.
*
* @param $id
* @return array
* @throws SmartEmailingException
*/
public function deleteContact($id)
{
return $this->call(self::METHOD_DELETE, self::NODE_CONTACT_FORGET . "/" . $id);
}
/**
* @return array
* @throws SmartEmailingException
*/
public function getContacts()
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTS);
}
/**
* @return array
* @throws SmartEmailingException
*/
public function getContact($id)
{
return $this->call(self::METHOD_GET, self::NODE_CONTACTS . "/" . $id);
}
/**
* https://app.smartemailing.cz/docs/api/v3/index.html#api-Import-Import_contacts
*
* @param $email
* @param array|NULL $contactLists
* @param array|NULL $properties
* @param array|NULL $customFields
* @param array|NULL $purposes
* @param array|NULL $settings
*/
public function importContact($email, array $contactLists = NULL, array $properties = NULL, array $customFields = NULL, array $purposes = NULL, array $settings = NULL)
{
$contact = [
'emailaddress' => $email,
];
if (is_array($contactLists)) {
$contact['contactlists'] = [];
foreach ($contactLists as $id => $status) {
$contact['contactlists'][] = [
'id' => $id,
'status' => $status,
];
}
}
if (is_array($properties)) {
foreach ($properties as $name => $value) {
$contact[$name] = $value;
}
}
if (is_array($customFields)) {
$contact['customfields'] = $customFields;
}
if (is_array($purposes)) {
$contact['purposes'] = $purposes;
}
$data = [
'data' => [
$contact,
],
];
if (is_array($settings)) {
$data['settings'] = $settings;
}
return $this->call(self::METHOD_POST, self::NODE_IMPORT, $data);
}
/**
* connect to Smartemailing API v3
*
* @param string $method
* @param string $node
* @param array|null $data
* @param array|null $query
* @return array
* @throws SmartEmailingException
*/
protected function call($method, $node, $data = NULL, $query = NULL)
{
$options = [
'auth' => [$this->username, $this->token],
];
if (is_array($data)) {
$options['json'] = $data;
}
if (is_array($query)) {
$options['query'] = $query;
}
$client = new Client();
try {
$response = $client->request($method, $this->url . "/" . $node, $options);
} catch (ClientException $e) {
$response = $e->getResponse();
$body = Json::decode((string) $response->getBody(), Json::FORCE_ARRAY);
throw new SmartEmailingException($body['message'], $response->getStatusCode());
}
return Json::decode((string) $response->getBody(), Json::FORCE_ARRAY);
}
}