SMTP check code in PHP – Download
Class file in PHP to check if email exists. Uses fsockopen and SMTP commands like HELO, MAIL, RCPT to check validity of emails. The attachment is a PHP class file.
Please remove txt from extension after downloading.
Class file in PHP to check if email exists. Uses fsockopen and SMTP commands like HELO, MAIL, RCPT to check validity of emails. The attachment is a PHP class file.
Please remove txt from extension after downloading.
Copyright © 2012 PHP, Web and IT stuff All rights reserved. PHP Web development in London.
cool
Is there an updated version of the email validate class? Or some documentation about how the author implemented it?
I have tried a few ways. Right now, passing an array holding the email addys to be tested, and another holding the ‘from email values’ in an array, and my result set is such that the first element and the last element have value (1 in both) and the rest is the list of email addresses as the key, but no value.
It is a great idea, and I really would like to use it.
TIA
I try your code, but it’s error: Fatal error: Cannot re-assign $this in C:\Program Files\xampp\php\pear\Net\DNS\RR.php on line 60.
How fixed it!
Cheers!
It works great!! Thanks.
Thanks for writing this. I’ve done this in a terminal for years but having the PHP front-end is very useful. I’ve found the Google public DNS service nameservers useful to use with this script too: http://code.google.com/speed/public-dns/
i want the code for email authentication.
how can i check that an email id is exists or not.
pls reply me soon.
thanks
please explain how to implement check mail code
Didn’t get a chance to try it, but question, where do I put the email that I need it to check? which variable, I see you made an array, but hwo do I use it?
I would glad if you answer me.
hi its good but how to know the smtp_server ipadresss can any one tell me with examples of placing
appreciate and thanks in advance…….
Does this exist?
Hi
It tool me a while but have found out how to use this class I had to make a tweek.
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);
}
$i=0;
// query the MTAs on each Domain
foreach($this->domains as $domain=>$users) {
$mxs = array();
// retrieve SMTP Server via MX query on domain
list($hosts, $mxweights) = $this->queryMX($domain);
if(count($hosts) > 0){
// retrieve MX priorities
for($n=0; $n domain);
$this->debug(print_r($mxs, 1));
$timeout = $this->max_conn_time/count($hosts);
// 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(“<<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(‘/^([0-9]{3}) /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;
}
}
// quit
$this->send(“quit”);
// close socket
fclose($this->sock);
}
}else{
$results[$this->domains[$domain][$i].’@’.$domain] = false;
}
$i++;
}
return $results;
}
function send($msg) {
fwrite($this->sock, $msg.”\r\n”);
$reply = fread($this->sock, 2082);
$this->debug(“>>>\n$msg\n”);
$this->debug(“<<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);
}
}
}
$checkEmail = new SMTP_validateEmail();
$check = $checkEmail->validate(array(“wtucke01@hotmail.com”), ‘warren.tucker@o-bit.net’);
print $check["wtucke01@hotmail.com"];
Did anyone ever figure out this code? I’m trying to also figure out where to put the email you are trying to validate but I’m not able to find where to put it. Also, Warren, I pasted your code into Dreamweaver and got tons of red flags for “,’, and {} out of place. If someone could PLEASE help me I’d appreciate it.
[...] 1) SMTP check code in PHP – DOWNLOAD [...]
good script
I tried the script and it works well on linux. However, I have some issues with making it work on Windows. Anything that I am missing? I added DNS.php to the Net folder inside PEAR folder. Please let me know if anyone has any idea about this issue.
Hey,
I’m not if this solves my problem. Say,I have a web and I have a registration form which I want users to fill their e-mails. Does the codes you provide validate and verify the e-mails AUTOMATICALLY?
If not so, how can I go around that?
Thanks,in advance.
Hi there.
First i want to thank you for this great article.
I’m trying to use this code but i am always getting this results:
array(2) { ["ysf654654654654564644@hotmail.com"]=> bool(false) ["ysf44@hotmail.com"]=> bool(false) }
ysf654654654654564644@hotmail.com,ysf44@hotmail.com is valid
even tho the 1st email is not valid, can anyone help me please?
hohoh