Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * PHP E-Mail Advanced Validation
- *
- * Coded by Christian Ditaputratama < ditatompel [at] gmail [dot] com >
- *
- * This program used to check whether e-mail address is valid.
- * The validation start from email address format, MX record, and SMTP
- * mailbox check.
- *
- * Please note that some mail server accept all mail although the mailbox
- * recipient does not exists!
- *
- * LICENSE :
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published by
- * the Free Software Foundation.
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * Usage :
- * check_email($email); // for email format and MX record validation.
- * check_email($email, FALSE); // for email format validation only.
- * check_email($email, TRUE, TRUE); // for email format, MX record and SMTP
- * Mailbox validation.
- */
- // Function to get SMTP respond code
- function serverRespond($respond) {
- return substr($respond, 0, 3);
- }
- // function to check whether mailbox is exists
- // return bool
- function checkSMTP($smtpHost, $emailAddr) {
- if ( !($socket = fsockopen($smtpHost, 25, $errno, $errstr, 15)) )
- return FALSE;
- if ( serverRespond(fgets($socket, 256)) != '220' )
- return FALSE;
- fwrite($socket, 'HELO ' . $smtpHost . "\r\n");
- if ( serverRespond(fgets($socket, 256)) != '250' )
- return FALSE;
- fwrite($socket, 'MAIL FROM: <mailvalidator@devilzc0de.org>' . "\r\n");
- if ( serverRespond(fgets($socket, 256)) != '250' )
- return FALSE;
- fwrite($socket, 'RCPT TO: <' . $emailAddr . '>'."\r\n");
- if ( serverRespond(fgets($socket, 256)) != '250' )
- return FALSE;
- fwrite($socket, 'QUIT' . "\r\n");
- fclose($socket);
- return TRUE;
- }
- // main email validation function
- // return bool
- function check_email($email, $cekMXRecord=1, $checkSMTPBox=0) {
- // check if the email address format is valid.
- if( !preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $domain) ) {
- return FALSE;
- }
- // check MX DNS record if $cekMXRecord set to 1
- if ( $cekMXRecord && function_exists('checkdnsrr') ) {
- if( !checkdnsrr($domain[1] . '.', 'MX') )
- return FALSE;
- // Check SMTP Mailbox is exists if $checkSMTPBox set to 1
- if ( $checkSMTPBox && function_exists('dns_get_record') ) {
- $smtpTarget = dns_get_record($domain[1], DNS_MX);
- return checkSMTP($smtpTarget[0]['target'], $email);
- }
- }
- return TRUE;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement