Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Validate email syntax
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- return FALSE;
- }
- // Extract domain portion of email address
- $parts = explode('@', $email);
- $domain = array_pop($parts);
- // Look up MX for domain
- if (!getmxrr($domain, $mxHosts)) {
- return FALSE;
- }
- // Loop MX hosts until we find one we can connect to
- foreach ($mxHosts as $host) {
- if (ip2long($host) !== FALSE) {
- // $host is already an IP - should never happen but just in case
- $ip = $host;
- } else {
- // Lookup IP for $host
- $ip = gethostbyname($host);
- if ($ip === $host) {
- continue;
- }
- }
- // Try and connect
- if (!$sock = fsockopen($ip, 25, $errNo, $errStr, $socketTimeout)) {
- continue;
- }
- // Set timeout for read ops
- $secs = floor($socketTimeout);
- $usecs = $socketTimeout - $secs;
- stream_set_timeout($sock, $secs, $usecs);
- }
- // If we haven't made a connection we've failed! Must do better.
- if (!$sock) {
- return FALSE;
- }
- // Have a little chat in SMTP. If any operation fails, consider the email address invalid
- while ($response = fgets($sock)) { // Should be a 220 intro
- if (substr($response, 0, 3) !== '220') {
- return FALSE;
- }
- if (trim($response[3]) === '') {
- break;
- }
- }
- if (!fwrite($sock, "HELO just.visiting\n")) { // Send HELO, should get a 250
- return FALSE;
- }
- while ($response = fgets($sock)) {
- if (substr($response, 0, 3) !== '250') {
- return FALSE;
- }
- if (trim($response[3]) === '') {
- break;
- }
- }
- if (!fwrite($sock, "MAIL FROM: <$fromAddress>\n")) { // MAIL FROM, should get a 250
- return FALSE;
- }
- while ($response = fgets($sock)) {
- if (substr($response, 0, 3) !== '250') {
- return FALSE;
- }
- if (trim($response[3]) === '') {
- break;
- }
- }
- if (!fwrite($sock, "RCPT TO: <$email>\n")) { // RCPT TO, should get a 250 if the address exists
- return FALSE;
- }
- while ($response = fgets($sock)) {
- if (substr($response, 0, 3) !== '250') {
- return FALSE;
- }
- if (trim($response[3]) === '') {
- break;
- }
- }
- fwrite($sock, "QUIT\n");
- fclose($sock);
- // Success!
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement