-
Notifications
You must be signed in to change notification settings - Fork 17
/
class.verifyEmail.php
207 lines (188 loc) · 6.32 KB
/
class.verifyEmail.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
<?php
/**
* Class to check up e-mail
*
* @author Konstantin Granin <[email protected]>
* @copyright Copyright (c) 2010, Konstantin Granin
*/
class verifyEmail {
/**
* User name
* @var string
*/
private $_fromName;
/**
* Domain name
* @var string
*/
private $_fromDomain;
/**
* SMTP port number
* @var int
*/
private $_port;
/**
* The connection timeout, in seconds.
* @var int
*/
private $_maxConnectionTimeout;
/**
* The timeout on socket connection
* @var int
*/
private $_maxStreamTimeout;
public function __construct() {
$this->_fromName = 'noreply';
$this->_fromDomain = 'localhost';
$this->_port = 25;
$this->_maxConnectionTimeout = 30;
$this->_maxStreamTimeout = 5;
}
/**
* Set email address for SMTP request
* @param string $email Email address
*/
public function setEmailFrom($email) {
list($this->_fromName, $this->_fromDomain) = $this->_parseEmail($email);
}
/**
* Set connection timeout, in seconds.
* @param int $seconds
*/
public function setConnectionTimeout($seconds) {
$this->_maxConnectionTimeout = $seconds;
}
/**
* Set the timeout on socket connection
* @param int $seconds
*/
public function setStreamTimeout($seconds) {
$this->_maxStreamTimeout = $seconds;
}
/**
* Validate email address.
* @param string $email
* @return boolean True if valid.
*/
public function isValid($email) {
return (false !== filter_var($email, FILTER_VALIDATE_EMAIL));
}
/**
* Get array of MX records for host. Sort by weight information.
* @param string $hostname The Internet host name.
* @return array Array of the MX records found.
*/
public function getMXrecords($hostname) {
$mxhosts = array();
$mxweights = array();
if (getmxrr($hostname, $mxhosts, $mxweights)) {
array_multisort($mxweights, $mxhosts);
}
/**
* Add A-record as last chance (e.g. if no MX record is there).
* Thanks Nicht Lieb.
*/
$mxhosts[] = $hostname;
return $mxhosts;
}
/**
* check up e-mail
* @param string $email Email address
* @return boolean True if the valid email also exist
*/
public function check($email) {
$result = false;
if ($this->isValid($email)) {
list($user, $domain) = $this->_parseEmail($email);
$mxs = $this->getMXrecords($domain);
$fp = false;
$timeout = ceil($this->_maxConnectionTimeout / count($mxs));
foreach ($mxs as $host) {
// if ($fp = @fsockopen($host, $this->_port, $errno, $errstr, $timeout)) {
if ($fp = @stream_socket_client("tcp://" . $host . ":" . $this->_port, $errno, $errstr, $timeout)) {
stream_set_timeout($fp, $this->_maxStreamTimeout);
stream_set_blocking($fp, 1);
// stream_set_blocking($fp, 0);
$code = $this->_fsockGetResponseCode($fp);
if ($code == '220') {
break;
} else {
fclose($fp);
$fp = false;
}
}
}
if ($fp) {
$this->_fsockquery($fp, "HELO " . $this->_fromDomain);
//$this->_fsockquery($fp, "VRFY " . $email);
$this->_fsockquery($fp, "MAIL FROM: <" . $this->_fromName . '@' . $this->_fromDomain . ">");
$code = $this->_fsockquery($fp, "RCPT TO: <" . $user . '@' . $domain . ">");
$this->_fsockquery($fp, "RSET");
$this->_fsockquery($fp, "QUIT");
fclose($fp);
if ($code == '250') {
/**
* http://www.ietf.org/rfc/rfc0821.txt
* 250 Requested mail action okay, completed
* email address was accepted
*/
$result = true;
} elseif ($code == '450' || $code == '451' || $code == '452') {
/**
* http://www.ietf.org/rfc/rfc0821.txt
* 450 Requested action not taken: the remote mail server
* does not want to accept mail from your server for
* some reason (IP address, blacklisting, etc..)
* Thanks Nicht Lieb.
* 451 Requested action aborted: local error in processing
* 452 Requested action not taken: insufficient system storage
* email address was greylisted (or some temporary error occured on the MTA)
* i believe that e-mail exists
*/
$result = true;
}
}
}
return $result;
}
/**
* Parses input string to array(0=>user, 1=>domain)
* @param string $email
* @return array
* @access private
*/
private function _parseEmail(&$email) {
return sscanf($email, "%[^@]@%s");
}
/**
* writes the contents of string to the file stream pointed to by handle $fp
* @access private
* @param resource $fp
* @param string $string The string that is to be written
* @return string Returns a string of up to length - 1 bytes read from the file pointed to by handle.
* If an error occurs, returns FALSE.
*/
private function _fsockquery(&$fp, $query) {
stream_socket_sendto($fp, $query . "\r\n");
return $this->_fsockGetResponseCode($fp);
}
/**
* Reads all the line long the answer and analyze it.
* @access private
* @param resource $fp
* @return string Response code
* If an error occurs, returns FALSE
*/
private function _fsockGetResponseCode(&$fp) {
$reply = stream_get_line($fp, 1);
$status = stream_get_meta_data($fp);
if ($status['unread_bytes']>0)
{
$reply .= stream_get_line($fp, $status['unread_bytes'],"\r\n");
}
preg_match('/^(?<code>[0-9]{3}) (.*)$/ims', $reply, $matches);
$code = isset($matches['code']) ? $matches['code'] : false;
return $code;
}
}
?>