-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathEmailValidationV4.php
executable file
·271 lines (227 loc) · 7.82 KB
/
EmailValidationV4.php
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
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Api;
use Exception;
use Mailgun\Assert;
use Mailgun\Model\EmailValidationV4\CreateBulkJobResponse;
use Mailgun\Model\EmailValidationV4\CreateBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\DeleteBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobsResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewsResponse;
use Mailgun\Model\EmailValidationV4\PromoteBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\ValidateResponse;
use Psr\Http\Message\ResponseInterface;
/**
* @see https://documentation.mailgun.com/en/latest/api-email-validation.html
*/
class EmailValidationV4 extends HttpApi
{
/**
* Addresses are validated based off defined checks.
*
* @param string $address An email address to validate. Maximum: 512 characters.
* @param bool $providerLookup A provider lookup will be performed if Mailgun’s internal analysis is insufficient
*
* @return ValidateResponse|ResponseInterface
*
* @throws Exception Thrown when we don't catch a Client or Server side Exception
*/
public function validate(string $address, bool $providerLookup = true)
{
Assert::stringNotEmpty($address);
$params = [
'address' => $address,
'provider_lookup' => $providerLookup,
];
$response = $this->httpGet('/v4/address/validate', $params);
return $this->hydrateResponse($response, ValidateResponse::class);
}
/**
* @param string $listId ID given when the list created
* @param mixed $filePath File path or file content
*
* @return mixed|ResponseInterface
*
* @throws Exception
*/
public function createBulkJob(string $listId, $filePath)
{
Assert::stringNotEmpty($listId);
if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) {
$fileData = ['filePath' => $filePath];
} else {
$fileData = [
'fileContent' => $filePath,
'filename' => 'file',
];
}
$postDataMultipart = [];
$postDataMultipart[] = $this->prepareFile('file', $fileData);
$response = $this->httpPostRaw(sprintf('/v4/address/validate/bulk/%s', $listId), $postDataMultipart);
$this->closeResources($postDataMultipart);
return $this->hydrateResponse($response, CreateBulkJobResponse::class);
}
/**
* @param string $listId ID given when the list created
*
* @return DeleteBulkJobResponse|ResponseInterface
*
* @throws Exception
*/
public function deleteBulkJob(string $listId)
{
Assert::stringNotEmpty($listId);
$response = $this->httpDelete(sprintf('/v4/address/validate/bulk/%s', $listId));
return $this->hydrateResponse($response, DeleteBulkJobResponse::class);
}
/**
* @param string $listId ID given when the list created
*
* @return GetBulkJobResponse|ResponseInterface
*
* @throws Exception
*/
public function getBulkJob(string $listId)
{
Assert::stringNotEmpty($listId);
$response = $this->httpGet(sprintf('/v4/address/validate/bulk/%s', $listId));
return $this->hydrateResponse($response, GetBulkJobResponse::class);
}
/**
* @param int $limit Jobs limit
*
* @return GetBulkJobsResponse|ResponseInterface
*
* @throws Exception
*/
public function getBulkJobs(int $limit = 500)
{
Assert::greaterThan($limit, 0);
$response = $this->httpGet('/v4/address/validate/bulk', [
'limit' => $limit,
]);
return $this->hydrateResponse($response, GetBulkJobsResponse::class);
}
/**
* @param int $limit Previews Limit
*
* @return mixed|ResponseInterface
*
* @throws Exception
*/
public function getBulkPreviews(int $limit = 500)
{
Assert::greaterThan($limit, 0);
$response = $this->httpGet('/v4/address/validate/preview', [
'limit' => $limit,
]);
return $this->hydrateResponse($response, GetBulkPreviewsResponse::class);
}
/**
* @param string $previewId ID given when the list created
* @param mixed $filePath File path or file content
*
* @return mixed|ResponseInterface
*
* @throws Exception
*/
public function createBulkPreview(string $previewId, $filePath)
{
Assert::stringNotEmpty($previewId);
if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) {
$fileData = ['filePath' => $filePath];
} else {
$fileData = [
'fileContent' => $filePath,
'filename' => 'file',
];
}
$postDataMultipart = [];
$postDataMultipart[] = $this->prepareFile('file', $fileData);
$response = $this->httpPostRaw(sprintf('/v4/address/validate/preview/%s', $previewId), $postDataMultipart);
$this->closeResources($postDataMultipart);
return $this->hydrateResponse($response, CreateBulkPreviewResponse::class);
}
/**
* @param string $previewId ID given when the list created
*
* @return mixed|ResponseInterface
*
* @throws Exception
*/
public function getBulkPreview(string $previewId)
{
Assert::stringNotEmpty($previewId);
$response = $this->httpGet(sprintf('/v4/address/validate/preview/%s', $previewId));
return $this->hydrateResponse($response, GetBulkPreviewResponse::class);
}
/**
* @param string $previewId ID given when the list created
*/
public function deleteBulkPreview(string $previewId): bool
{
Assert::stringNotEmpty($previewId);
$response = $this->httpDelete(sprintf('/v4/address/validate/preview/%s', $previewId));
return 204 === $response->getStatusCode();
}
/**
* @param string $previewId ID given when the list created
*
* @return mixed|ResponseInterface
*
* @throws Exception
*/
public function promoteBulkPreview(string $previewId)
{
Assert::stringNotEmpty($previewId);
$response = $this->httpPut(sprintf('/v4/address/validate/preview/%s', $previewId));
return $this->hydrateResponse($response, PromoteBulkPreviewResponse::class);
}
/**
* @param string $fieldName Field Name
* @param array $filePath ['fileContent' => 'content'] or ['filePath' => '/foo/bar']
*
* @return array File Data
*/
private function prepareFile(string $fieldName, array $filePath): array
{
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
$resource = null;
if (isset($filePath['fileContent'])) {
// File from memory
$resource = fopen('php://temp', 'r+');
fwrite($resource, $filePath['fileContent']);
rewind($resource);
} elseif (isset($filePath['filePath'])) {
// File form path
$path = $filePath['filePath'];
$resource = fopen($path, 'r');
}
return [
'name' => $fieldName,
'content' => $resource,
'filename' => $filename,
];
}
/**
* Close open resources.
*
* @param array $params Resource params
*/
private function closeResources(array $params): void
{
foreach ($params as $param) {
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
fclose($param['content']);
}
}
}
}