Advertisement
ditatompel

PHP E-Mail Advanced Validation

Oct 14th, 2012
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PHP E-Mail Advanced Validation
  4.  *
  5.  * Coded by Christian Ditaputratama < ditatompel [at] gmail [dot] com >
  6.  *
  7.  * This program used to check whether e-mail address is valid.
  8.  * The validation start from email address format, MX record, and SMTP
  9.  * mailbox check.
  10.  *
  11.  * Please note that some mail server accept all mail although the mailbox
  12.  * recipient does not exists!
  13.  *
  14.  * LICENSE :
  15.  * This program is free software; you can redistribute it and/or modify it
  16.  * under the terms of the GNU General Public License version 2 as published by
  17.  * the Free Software Foundation.
  18.  * This program is distributed in the hope that it will be useful, but WITHOUT
  19.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20.  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  21.  * more details.
  22.  *
  23.  * Usage :
  24.  * check_email($email); // for email format and MX record validation.
  25.  * check_email($email, FALSE); // for email format validation only.
  26.  * check_email($email, TRUE, TRUE); // for email format, MX record and SMTP
  27.  * Mailbox validation.
  28.  */
  29.  
  30. // Function to get SMTP respond code
  31. function serverRespond($respond) {
  32.     return substr($respond, 0, 3);
  33. }
  34.  
  35. // function to check whether mailbox is exists
  36. // return bool
  37. function checkSMTP($smtpHost, $emailAddr) {
  38.     if ( !($socket = fsockopen($smtpHost, 25, $errno, $errstr, 15)) )
  39.         return FALSE;
  40.     if ( serverRespond(fgets($socket, 256)) != '220' )
  41.         return FALSE;
  42.     fwrite($socket, 'HELO ' . $smtpHost . "\r\n");
  43.     if ( serverRespond(fgets($socket, 256)) != '250' )
  44.         return FALSE;
  45.     fwrite($socket, 'MAIL FROM: <mailvalidator@devilzc0de.org>' . "\r\n");
  46.     if ( serverRespond(fgets($socket, 256)) != '250' )
  47.         return FALSE;
  48.     fwrite($socket, 'RCPT TO: <' . $emailAddr . '>'."\r\n");
  49.     if ( serverRespond(fgets($socket, 256)) != '250' )
  50.         return FALSE;
  51.     fwrite($socket, 'QUIT' . "\r\n");
  52.     fclose($socket);
  53.     return TRUE;
  54. }
  55.  
  56. // main email validation function
  57. // return bool
  58. function check_email($email, $cekMXRecord=1, $checkSMTPBox=0) {
  59.     // check if the email address format is valid.
  60.     if( !preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $domain) ) {
  61.         return FALSE;
  62.     }
  63.    
  64.     // check MX DNS record if $cekMXRecord set to 1
  65.     if ( $cekMXRecord && function_exists('checkdnsrr') ) {
  66.         if( !checkdnsrr($domain[1] . '.', 'MX') )
  67.             return FALSE;
  68.        
  69.         // Check SMTP Mailbox is exists if $checkSMTPBox set to 1
  70.         if ( $checkSMTPBox && function_exists('dns_get_record') ) {
  71.             $smtpTarget = dns_get_record($domain[1], DNS_MX);
  72.             return checkSMTP($smtpTarget[0]['target'], $email);
  73.         }
  74.     }
  75.     return TRUE;
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement