1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Unicodeveloper\EmailValidator; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use QuickEmailVerification\Client; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
|
9
|
|
|
class EmailValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Various response messages based on verification status |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $serverResponses = [ |
16
|
|
|
'invalid_email' => 'Specified email has invalid email address syntax', |
17
|
|
|
'invalid_domain' => 'Domain name does not exist', |
18
|
|
|
'rejected_email' => 'SMTP server rejected email. Email does not exist', |
19
|
|
|
'accepted_email' => 'SMTP server accepted email address', |
20
|
|
|
'no_connect' => 'SMTP server connection failure', |
21
|
|
|
'timeout' => 'Session time out occurred at SMTP server', |
22
|
|
|
'unavailable_smtp' => 'SMTP server is not available to process request', |
23
|
|
|
'unexpected_error' => 'An unexpected error has occurred', |
24
|
|
|
'no_mx_record' => 'Could not get MX records for domain', |
25
|
|
|
'temporarily_blocked' => 'Email is temporarily greylisted', |
26
|
|
|
'exceeded_storage' => 'SMTP server rejected email. Exceeded storage allocation' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Api Key for quickemailverficationservice |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $apiKey; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Instance of QuickEmailVerification Client |
37
|
|
|
* @var object |
38
|
|
|
*/ |
39
|
|
|
public $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Response from the QuickEmailVerification Service |
43
|
|
|
* @var object |
44
|
|
|
*/ |
45
|
|
|
public $response; |
46
|
|
|
|
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
$this->setApiKey(); |
50
|
|
|
$this->setClient(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get apiKey from Config file |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function setApikey() |
58
|
|
|
{ |
59
|
|
|
$this->apiKey = Config::get('emailValidator.apiKey'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Instantiate QuickEmailVerification Client with api Key |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function setClient() |
67
|
|
|
{ |
68
|
|
|
if(empty($this->apiKey)) |
69
|
|
|
{ |
70
|
|
|
throw new InvalidArgumentException('Api key can not be empty'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->client = new Client($this->apiKey); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Verifies email Address and returns an API Object response |
78
|
|
|
* @param string $emailAddress |
79
|
|
|
* @return instance |
80
|
|
|
*/ |
81
|
|
|
public function verify($emailAddress) |
82
|
|
|
{ |
83
|
|
|
$this->response = $this->client->quickemailverification()->verify($emailAddress); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns an array with true/false and an appropriate message when doing the email verification |
90
|
|
|
* @return array |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
public function isValid() |
94
|
|
|
{ |
95
|
|
|
$status = $this->response->body['result']; |
96
|
|
|
$reason = $this->response->body['reason']; |
97
|
|
|
|
98
|
|
|
switch($status): |
99
|
|
|
case 'valid': |
100
|
|
|
return [true, $this->getReason($reason)]; |
101
|
|
|
break; |
|
|
|
|
102
|
|
|
case 'invalid': |
103
|
|
|
case 'unknown': |
104
|
|
|
return [false, $this->getReason($reason)]; |
105
|
|
|
break; |
|
|
|
|
106
|
|
|
default: |
107
|
|
|
throw new InvalidArgumentException('Invalid Response'); |
108
|
|
|
endSwitch; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the reason whether the email is valid, invalid or unknown |
113
|
|
|
* @param string $reason |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getReason($reason) |
117
|
|
|
{ |
118
|
|
|
return $this->serverResponses[$reason]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns true or false if the email address uses a disposable domain |
123
|
|
|
* @return boolean |
124
|
|
|
*/ |
125
|
|
|
public function isDisposable() |
126
|
|
|
{ |
127
|
|
|
return $this->response->body['disposable']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns true or false if the API request was successful |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
public function apiRequestStatus() |
135
|
|
|
{ |
136
|
|
|
return $this->response->body['success']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the domain of the provided email address |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getDomainName() |
144
|
|
|
{ |
145
|
|
|
return $this->response->body['domain']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the local part of an email address |
150
|
|
|
* Example: [email protected] returns prosperotemuyiwa |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getUser() |
154
|
|
|
{ |
155
|
|
|
return $this->response->body['user']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets a normalized version of the email address |
160
|
|
|
* Example: [email protected] returns [email protected] |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getEmailAddress() |
164
|
|
|
{ |
165
|
|
|
return $this->response->body['email']; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns true if the domain appears to accept all emails delivered to that domain |
170
|
|
|
* @return boolean |
171
|
|
|
*/ |
172
|
|
|
public function acceptEmailsDeliveredToDomain() |
173
|
|
|
{ |
174
|
|
|
return $this->response->body['accept_all']; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns true or false if email address is a role address |
179
|
|
|
* Example [email protected] , [email protected] will return true |
180
|
|
|
* @return boolean |
181
|
|
|
*/ |
182
|
|
|
public function isRole() |
183
|
|
|
{ |
184
|
|
|
return $this->response->body['role']; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: