|
1
|
|
|
<?PHP |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
|
5
|
|
|
* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Mailgun; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
13
|
|
|
use Http\Client\HttpClient; |
|
14
|
|
|
use Mailgun\Connection\RestClient; |
|
15
|
|
|
use Mailgun\Constants\ExceptionMessages; |
|
16
|
|
|
use Mailgun\Lists\OptInHandler; |
|
17
|
|
|
use Mailgun\Messages\BatchMessage; |
|
18
|
|
|
use Mailgun\Messages\Exceptions; |
|
19
|
|
|
use Mailgun\Messages\MessageBuilder; |
|
20
|
|
|
use Mailgun\Deserializer\ModelDeserializer; |
|
21
|
|
|
use Mailgun\Deserializer\ResponseDeserializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This class is the base class for the Mailgun SDK. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Mailgun |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var RestClient |
|
30
|
|
|
* |
|
31
|
|
|
* @depracated Will be removed in 3.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $restClient; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $apiKey; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var HttpMethodsClient |
|
42
|
|
|
*/ |
|
43
|
|
|
private $httpClient; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var ResponseDeserializer |
|
47
|
|
|
*/ |
|
48
|
|
|
private $deserializer; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var RequestBuilder |
|
52
|
|
|
*/ |
|
53
|
|
|
private $requestBuilder; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string|null $apiKey |
|
57
|
|
|
* @param HttpClient|null $httpClient |
|
58
|
|
|
* @param string $apiEndpoint |
|
59
|
|
|
* @param ResponseDeserializer|null $deserializer |
|
60
|
|
|
* @param HttpClientConfigurator|null $clientConfigurator |
|
61
|
|
|
* @param RequestBuilder|null $requestBuilder |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function __construct( |
|
64
|
|
|
$apiKey = null, |
|
65
|
|
|
HttpClient $httpClient = null, /* Deprecated, will be removed in 3.0 */ |
|
66
|
|
|
$apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */ |
|
67
|
|
|
ResponseDeserializer $deserializer = null, |
|
68
|
|
|
HttpClientConfigurator $clientConfigurator = null, |
|
69
|
|
|
RequestBuilder $requestBuilder = null |
|
70
|
|
|
) { |
|
71
|
4 |
|
$this->apiKey = $apiKey; |
|
72
|
4 |
|
$this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); |
|
73
|
|
|
|
|
74
|
4 |
|
if (null === $clientConfigurator) { |
|
75
|
4 |
|
$clientConfigurator = new HttpClientConfigurator(); |
|
76
|
|
|
|
|
77
|
|
|
/* |
|
78
|
|
|
* To be backward compatible |
|
79
|
|
|
*/ |
|
80
|
4 |
|
if ($apiEndpoint !== 'api.mailgun.net') { |
|
81
|
|
|
$clientConfigurator->setEndpoint($apiEndpoint); |
|
82
|
|
|
} |
|
83
|
4 |
|
if ($httpClient !== null) { |
|
84
|
|
|
$clientConfigurator->setHttpClient($httpClient); |
|
85
|
|
|
} |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
$clientConfigurator->setApiKey($apiKey); |
|
89
|
|
|
|
|
90
|
4 |
|
$this->httpClient = $clientConfigurator->createConfiguredClient(); |
|
|
|
|
|
|
91
|
4 |
|
$this->requestBuilder = $requestBuilder ?: new RequestBuilder(); |
|
92
|
4 |
|
$this->deserializer = $deserializer ?: new ModelDeserializer(); |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* This function allows the sending of a fully formed message OR a custom |
|
97
|
|
|
* MIME string. If sending MIME, the string must be passed in to the 3rd |
|
98
|
|
|
* position of the function call. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $workingDomain |
|
101
|
|
|
* @param array $postData |
|
102
|
|
|
* @param array $postFiles |
|
103
|
|
|
* |
|
104
|
|
|
* @throws Exceptions\MissingRequiredMIMEParameters |
|
105
|
|
|
* |
|
106
|
|
|
* @return \stdClass |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function sendMessage($workingDomain, $postData, $postFiles = []) |
|
109
|
|
|
{ |
|
110
|
6 |
|
if (is_array($postFiles)) { |
|
111
|
4 |
|
return $this->post("$workingDomain/messages", $postData, $postFiles); |
|
112
|
2 |
|
} elseif (is_string($postFiles)) { |
|
113
|
1 |
|
$tempFile = tempnam(sys_get_temp_dir(), 'MG_TMP_MIME'); |
|
114
|
1 |
|
$fileHandle = fopen($tempFile, 'w'); |
|
115
|
1 |
|
fwrite($fileHandle, $postFiles); |
|
116
|
|
|
|
|
117
|
1 |
|
$result = $this->post("$workingDomain/messages.mime", $postData, ['message' => $tempFile]); |
|
118
|
1 |
|
fclose($fileHandle); |
|
119
|
1 |
|
unlink($tempFile); |
|
120
|
|
|
|
|
121
|
1 |
|
return $result; |
|
122
|
|
|
} else { |
|
123
|
1 |
|
throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* This function checks the signature in a POST request to see if it is |
|
129
|
|
|
* authentic. |
|
130
|
|
|
* |
|
131
|
|
|
* Pass an array of parameters. If you pass nothing, $_POST will be |
|
132
|
|
|
* used instead. |
|
133
|
|
|
* |
|
134
|
|
|
* If this function returns FALSE, you must not process the request. |
|
135
|
|
|
* You should reject the request with status code 403 Forbidden. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array|null $postData |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
3 |
|
public function verifyWebhookSignature($postData = null) |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
3 |
|
if ($postData === null) { |
|
144
|
|
|
$postData = $_POST; |
|
145
|
|
|
} |
|
146
|
3 |
|
if (!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { |
|
147
|
1 |
|
return false; |
|
148
|
|
|
} |
|
149
|
2 |
|
$hmac = hash_hmac('sha256', "{$postData['timestamp']}{$postData['token']}", $this->apiKey); |
|
150
|
2 |
|
$sig = $postData['signature']; |
|
151
|
2 |
|
if (function_exists('hash_equals')) { |
|
152
|
|
|
// hash_equals is constant time, but will not be introduced until PHP 5.6 |
|
153
|
|
|
return hash_equals($hmac, $sig); |
|
154
|
|
|
} else { |
|
155
|
2 |
|
return $hmac === $sig; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $endpointUrl |
|
161
|
|
|
* @param array $postData |
|
162
|
|
|
* @param array $files |
|
163
|
|
|
* |
|
164
|
|
|
* @return \stdClass |
|
165
|
|
|
*/ |
|
166
|
7 |
|
public function post($endpointUrl, $postData = [], $files = []) |
|
167
|
|
|
{ |
|
168
|
7 |
|
return $this->restClient->post($endpointUrl, $postData, $files); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $endpointUrl |
|
173
|
|
|
* @param array $queryString |
|
174
|
|
|
* |
|
175
|
|
|
* @return \stdClass |
|
176
|
|
|
*/ |
|
177
|
|
|
public function get($endpointUrl, $queryString = []) |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->restClient->get($endpointUrl, $queryString); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $endpointUrl |
|
184
|
|
|
* |
|
185
|
|
|
* @return \stdClass |
|
186
|
|
|
*/ |
|
187
|
|
|
public function delete($endpointUrl) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->restClient->delete($endpointUrl); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $endpointUrl |
|
194
|
|
|
* @param array $putData |
|
195
|
|
|
* |
|
196
|
|
|
* @return \stdClass |
|
197
|
|
|
*/ |
|
198
|
|
|
public function put($endpointUrl, $putData) |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->restClient->put($endpointUrl, $putData); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param string $apiVersion |
|
205
|
|
|
* |
|
206
|
|
|
* @return Mailgun |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setApiVersion($apiVersion) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->restClient->setApiVersion($apiVersion); |
|
211
|
|
|
|
|
212
|
|
|
return $this; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param bool $sslEnabled |
|
217
|
|
|
* |
|
218
|
|
|
* @return Mailgun |
|
219
|
|
|
* |
|
220
|
|
|
* @deprecated This will be removed in 3.0. Mailgun does not support non-secure connections to their API. |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setSslEnabled($sslEnabled) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->restClient->setSslEnabled($sslEnabled); |
|
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return MessageBuilder |
|
231
|
|
|
*/ |
|
232
|
28 |
|
public function MessageBuilder() |
|
233
|
|
|
{ |
|
234
|
28 |
|
return new MessageBuilder(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return OptInHandler |
|
239
|
|
|
*/ |
|
240
|
3 |
|
public function OptInHandler() |
|
241
|
|
|
{ |
|
242
|
3 |
|
return new OptInHandler(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param string $workingDomain |
|
247
|
|
|
* @param bool $autoSend |
|
248
|
|
|
* |
|
249
|
|
|
* @return BatchMessage |
|
250
|
|
|
*/ |
|
251
|
15 |
|
public function BatchMessage($workingDomain, $autoSend = true) |
|
252
|
|
|
{ |
|
253
|
15 |
|
return new BatchMessage($this->restClient, $workingDomain, $autoSend); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return Api\Stats |
|
258
|
|
|
*/ |
|
259
|
|
|
public function stats() |
|
260
|
|
|
{ |
|
261
|
|
|
return new Api\Stats($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @return Api\Domain |
|
266
|
|
|
*/ |
|
267
|
|
|
public function domains() |
|
268
|
|
|
{ |
|
269
|
|
|
return new Api\Domain($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @return Api\Event |
|
274
|
|
|
*/ |
|
275
|
|
|
public function events() |
|
276
|
|
|
{ |
|
277
|
|
|
return new Api\Event($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return Api\Routes |
|
282
|
|
|
*/ |
|
283
|
|
|
public function routes() |
|
284
|
|
|
{ |
|
285
|
|
|
return new Api\Routes($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return Api\Webhook |
|
290
|
|
|
*/ |
|
291
|
|
|
public function webhooks() |
|
292
|
|
|
{ |
|
293
|
|
|
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @return Api\Message |
|
298
|
|
|
*/ |
|
299
|
|
|
public function messages() |
|
300
|
|
|
{ |
|
301
|
|
|
return new Api\Message($this->httpClient, $this->requestBuilder, $this->deserializer); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..