- Saved searches
- Use saved searches to filter your results more quickly
- License
- ddtraceweb/smtp-validator-email
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- PHP Email Address validation through SMTP
- Saved searches
- Use saved searches to filter your results more quickly
- msalahat/php-smtp-email-validation
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
valid your emails with smtp protocol, check mx and more.
License
ddtraceweb/smtp-validator-email
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
#Installation For installation you can use composer.
Simple add «ddtraceweb/smtp-validator-email»: «dev-master» in your composer.json to install the latest version
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = 'toto@somewhererlse.com'; $validator = new ValidatorEmail($email, $from); var_dump($validator->getResults()); ?>
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = array('toto@somewhererlse.com', 'titi@totitito.com'); $validator = new ValidatorEmail($email, $from); var_dump($validator->getResults()); ?>
- example with X emails and have custom delays time when connection and send HELO, with domain need time to respond.
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = array('toto@somewhererlse.com', 'titi@totitito.com'); //two loops in this example for difficult domains. $options = array('delaySleep' => array(0, 6)); //Handle $options to the constructor as third parameter $validator = new ValidatorEmail($email, $from, $options); var_dump($validator->getResults()); ?>
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = array('toto@somewhererlse.com', 'titi@totitito.com'); //more informations option activate $options = array('domainMoreInfo' => true); //Handle $options to the constructor as third parameter $validator = new ValidatorEmail($email, $from, $options); var_dump($validator->getResults()); ?>
- example with X emails with more informations on domain, mxs and priorities. In example same domain for two email. This is a connection to domain and check all account emails.
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = array('toto@somewhererlse.com', 'titi@somewhererlse.com'); //more informations option activate $options = array('domainMoreInfo' => true); //Handle $options to the constructor as third parameter $validator = new ValidatorEmail($email, $from, $options); var_dump($validator->getResults()); ?>
use SmtpValidatorEmail\ValidatorEmail; $from = 'xyz@xzzz.com'; // for SMTP FROM:<> command $emails = 'toto@somewhererlse.com'; $validator = new ValidatorEmail($email, $from, array('debug' => true, 'context' => 'socket' => array('bindto' => '0.0.0.0'))); var_dump($validator->getResults()); var_dump($validator->getDebug()); ?>
array( 'domainMoreInfo' => false, 'delaySleep' => array(0), 'noCommIsValid' => 0, 'catchAllIsValid' => 0, 'catchAllEnabled' => 1, 'timeout' => null, // ini_get("default_socket_timeout") 'context' => array(), 'detailResults' => false, // Instead of returning 0 for invalid and 1 for valid, it will return an array. array('result' => $isValid /* 0 or 1 */, 'info' => "") 'debug' => false );
About
valid your emails with smtp protocol, check mx and more.
PHP Email Address validation through SMTP
Here is a PHP class written for PHP4 and PHP5 that will validate email addresses by querying the SMTP (Simple Mail Transfer Protocol) server. This is meant to complement validation of the syntax of the email address, which should be used before validating the email via SMTP, which is more resource and time consuming. For more information read this thread:
http://www.daniweb.com/forums/thread141944.html Documentation and updates:
http://onwebdevelopment.blogspot.com/2008/08/php-email-address-validation-through.html Source code:
Source code has been moved:
http://code.google.com/p/php-smtp-email-validation/
setEmails($emails); > if ($sender) < $this->setSenderEmail($sender); > > function _parseEmail($email) < $parts = explode('@', $email); $domain = array_pop($parts); $user= implode('@', $parts); return array($user, $domain); >/** * Set the Emails to validate * @param $emails Array List of Emails */ function setEmails($emails) < foreach($emails as $email) < list($user, $domain) = $this->_parseEmail($email); if (!isset($this->domains[$domain])) < $this->domains[$domain] = array(); > $this->domains[$domain][] = $user; > > /** * Set the Email of the sender/validator * @param $email String */ function setSenderEmail($email) < $parts = $this->_parseEmail($email); $this->from_user = $parts[0]; $this->from_domain = $parts[1]; > /** * Validate Email Addresses * @param String $emails Emails to validate (recipient emails) * @param String $sender Sender's Email * @return Array Associative List of Emails and their validation results */ function validate($emails = false, $sender = false) < $results = array(); if ($emails) < $this->setEmails($emails); > if ($sender) < $this->setSenderEmail($sender); > // query the MTAs on each Domain foreach($this->domains as $domain=>$users) < $mxs = array(); // current domain being queried $this->domain = $domain; // retrieve SMTP Server via MX query on domain list($hosts, $mxweights) = $this->queryMX($domain); // retrieve MX priorities for($n=0; $n < count($hosts); $n++)< $mxs[$hosts[$n]] = $mxweights[$n]; >asort($mxs); // last fallback is the original domain $mxs[$this->domain] = 0; $this->debug(print_r($mxs, 1)); $timeout = $this->max_conn_time; // try each host while(list($host) = each($mxs)) < // connect to SMTP server $this->debug("try $host:$this->port\n"); if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) < stream_set_timeout($this->sock, $this->max_read_time); break; > > // did we get a TCP socket if ($this->sock) < $reply = fread($this->sock, 2082); $this->debug("<<<\n$reply"); preg_match('/^(8) /ims', $reply, $matches); $code = isset($matches[1]) ? $matches[1] : ''; if($code != '220') < // MTA gave an error. foreach($users as $user) < $results[$user.'@'.$domain] = false; >continue; > // say helo $this->send("HELO ".$this->from_domain); // tell of sender $this->send("MAIL FROM: from_user.'@'.$this->from_domain.">"); // ask for each recepient on this domain foreach($users as $user) < // ask of recepient $reply = $this->send("RCPT TO: "); // get code and msg from response preg_match('/^(4) /ims', $reply, $matches); $code = isset($matches[1]) ? $matches[1] : ''; if ($code == '250') < // you received 250 so the email address was accepted $results[$user.'@'.$domain] = true; >elseif ($code == '451' || $code == '452') < // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok $results[$user.'@'.$domain] = true; >else < $results[$user.'@'.$domain] = false; >> // reset before quit $this->send("RSET"); // quit $this->send("quit"); // close socket fclose($this->sock); > > return $results; > function send($msg) < fwrite($this->sock, $msg."\r\n"); $reply = fread($this->sock, 2082); $this->debug(">>>\n$msg\n"); $this->debug(" <<<\n$reply"); return $reply; >/** * Query DNS server for MX entries * @return */ function queryMX($domain) < $hosts = array(); $mxweights = array(); if (function_exists('getmxrr')) < getmxrr($domain, $hosts, $mxweights); >else < // windows, we need Net_DNS require_once 'Net/DNS.php'; $resolver = new Net_DNS_Resolver(); $resolver->debug = $this->debug; // nameservers to query $resolver->nameservers = $this->nameservers; $resp = $resolver->query($domain, 'MX'); if ($resp) < foreach($resp->answer as $answer) < $hosts[] = $answer->exchange; $mxweights[] = $answer->preference; > > > return array($hosts, $mxweights); > /** * Simple function to replicate PHP 5 behaviour. http://php.net/microtime */ function microtime_float() < list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); >function debug($str) < if ($this->debug) < echo '
'.htmlentities($str).''; > > > ?>
Awesome snippet. No longer to webmasters have to sift through a SQL database full of emails such as random@thisisnotahost.com.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Automatically exported from code.google.com/p/php-smtp-email-validation
msalahat/php-smtp-email-validation
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
** A PHP class that allows validation of email addresses via SMTP.
Email Syntax can check whether an email address has a valid format, however, in order to check if the email address actually exists, you will have to query the email domain for the existence of the email address.
The PHP class encapsulates the SMTP transation between the remote domain, as well as the DNS lookup for the Mail Transfer Agent (MTA) responsible for that domain.
The class can take a number of email addresses, and return whether they are valid or not. It will group together emails with the same domain, and create a single SMTP connection to the MTA for those emails for efficiency.
This class is meant to be a secondary validation of an email address after the email syntax is validated. It can also provide intermediate email validation before sending a confirmation email to the email address, which does not provide user feedback if the email is invalid — and thus loss of users.
Example Usage Validating a Single Email address:
// include SMTP Email Validation Class require_once('smtp_validateEmail.class.php'); // the email to validate $email = 'user@example.com'; // an optional sender $sender = 'user@mydomain.com'; // instantiate the class $SMTP_Validator = new SMTP_validateEmail(); // turn on debugging if you want to view the SMTP transaction $SMTP_Validator->debug = true; // do the validation $results = $SMTP_Validator->validate(array($email), $sender); // view results echo $email.' is '.($results[$email] ? 'valid' : 'invalid')."\n"; // send email? if ($results[$email]) < //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\r\n"); // send email >elseValidating Multiple Email addresses:
// include SMTP Email Validation Class require_once('smtp_validateEmail.class.php'); // the email to validate $emails = array('user@example.com', 'user2@example.com'); // an optional sender $sender = 'user@yourdomain.com'; // instantiate the class $SMTP_Validator = new SMTP_validateEmail(); // turn on debugging if you want to view the SMTP transaction $SMTP_Validator->debug = true; // do the validation $results = $SMTP_Validator->validate($emails, $sender); // view results foreach($results as $email=>$result) < // send email? if ($result) < //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\r\n"); // send email >else < echo 'The email address '. $email.' is not valid'; >>
About
Automatically exported from code.google.com/p/php-smtp-email-validation