php email adresinin smtp ile doğruluğunu kontrol etmek email validator
PHP Kod: Kodu kopyalamak için üzerine çift tıklayın!
CLASS DOSYASI
<?php
class SMTP_validateEmail {
/**
* PHP Socket resource to remote MTA
* @var resource $sock
*/
var $sock;
/**
* Current User being validated
*/
var $user;
/**
* Current domain where user is being validated
*/
var $domain;
/**
* List of domains to validate users on
*/
var $domains;
/**
* SMTP Port
*/
var $port = 25;
/**
* Maximum Connection Time to an MTA
*/
var $max_conn_time = 30;
/**
* Maximum time to read from socket
*/
var $max_read_time = 5;
/**
* username of sender
*/
var $from_user = 'user';
/**
* Host Name of sender
*/
var $from_domain = 'localhost';
/**
* Nameservers to use when make DNS query for MX entries
* @var Array $nameservers
*/
var $nameservers = array(
'192.168.0.1'
);
var $debug = false;
/**
* Initializes the Class
* [MENTION=27818]return[/MENTION] SMTP_validateEmail Instance
* [MENTION=81769]para[/MENTION]m $email Array[optional] List of Emails to Validate
* [MENTION=81769]para[/MENTION]m $sender String[optional] Email of validator
*/
function SMTP_validateEmail($emails = false, $sender = false) {
if ($emails) {
$this->setEmails($emails);
}
if ($sender) {
$this->setSenderEmail($sender);
}
}
/**
* Set the Emails to validate
* [MENTION=81769]para[/MENTION]m $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
* [MENTION=81769]para[/MENTION]m $email String
*/
function setSenderEmail($email) {
$parts = $this->_parseEmail($email);
$this->from_user = $parts[0];
$this->from_domain = $parts[1];
}
/**
* Validate Email Addresses
* [MENTION=81769]para[/MENTION]m String $emails Emails to validate (recipient emails)
* [MENTION=81769]para[/MENTION]m String $sender Sender's Email
* [MENTION=27818]return[/MENTION] 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();
// retrieve SMTP Server via MX query on domain
list($hosts, $mxweights) = $this->queryMX($domain);
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: <".$this->from_user.'@'.$this->from_domain.">");
// ask for each recepient on this domain
foreach($users as $user) {
// ask of recepient
$reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
// 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);
}
}
return $results;
}
function send($msg) {
fwrite($this->sock, $msg."\r\n");
/**
* Query DNS server for MX entries
* [MENTION=27818]return[/MENTION]
*/
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. [url]http://php.net/microtime[/url]
*/
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function debug($str) {
if ($this->debug) {
echo htmlentities($str);
}
}
}
?>
KULLANIMI :
< ?php
// the email to validate
$email = 'joe[MENTION=122995]Gmail[/MENTION].com';
// an optional sender
$sender = 'user[MENTION=125882]Example[/MENTION].com';
// instantiate the class
$SMTP_Valid = new SMTP_validateEmail();
// do the validation
$result = $SMTP_Valid->validate($email, $sender);
// view results
var_dump($result);
echo $email.' is '.($result ? 'valid' : 'invalid')."\n";