| | |
check if email address is correct without sending email?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
hi all,
i m working on the email validation i ve to check email id either it is correct or not but i cannt send mail on them. ie when i validate all email id client send newsletter and they should not bounce.
thanx in advance
i m working on the email validation i ve to check email id either it is correct or not but i cannt send mail on them. ie when i validate all email id client send newsletter and they should not bounce.
thanx in advance
Help as an alias
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
Here's a function I use sometimes. It explodes the address to get the domain name. It then checks for a Mail Exchange on the domain. This will work for bogus domain names but won't do anything for email addresses like
bill@hotmail.com or suzie@yahoo.com . Try to keep in mind, there is no sure-fire way to authenticate an email address without an email and a corresponding response. php Syntax (Toggle Plain Text)
function EmailValidation($email) { $email = htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits if ( eregi ( '[a-z||0-9]@[a-z||0-9].[a-z]', $email ) ) { //checks to make sure the email address is in a valid format $domain = explode( "@", $email ); //get the domain name if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) { //if the connection can be established, the email address is probably valid return true; } else { return false; //if a connection cannot be established return false } return false; //if email address is an invalid format return false } }
Lost time is never found again.
- Benjamin Franklin
- Benjamin Franklin
•
•
•
•
hi all,
i m working on the email validation i ve to check email id either it is correct or not but i cannt send mail on them. ie when i validate all email id client send newsletter and they should not bounce.
thanx in advance
To make sure the email address exsits, you will have to make an actual SMTP connection to the remote host/domain you want to send email to, and check if the email address is rejected or not. Usually, if it isn't rejected, then it means the email will be received by the server, this still doesn't mean it exists or the email will actually reach its recipient, since it could be routed to a catch all email or just trashed depending on the setup of the remote SMTP server.
It just means that the email you send will not bounce.
Here is an example of an SMTP connection:
http://www.yuki-onna.co.uk/email/smtp.html
In PHP, you'll just have to make a TCP connection to the remove SMTP server, and send it the SMTP commands up to the:
PHP Syntax (Toggle Plain Text)
RCPT TO: mail@otherdomain.ext
If the server rejects the email account it will reply with something other then "250 ok".
A simple example:
Lets say the email address is user@example.com
php Syntax (Toggle Plain Text)
$email = 'user@example.com'; list($user, $domain) = explode('@', $email); $port = 25; // default smtp port $sock = fsockopen($domain, $port); if ($sock) { fputs($sock, 'HELO mydomain.com'); $reply = fgets($sock); // not interesting fputs($sock, 'MAIL FROM: user@mydomain.com'); $reply = fgets($sock); // not interesting fputs($sock, 'RCPT TO: '.$email); $reply = fgets($sock); // interesting list($code, $msg) = explode(' ', $reply); if ($code == '250') { // you received 250 so the email address is accepted } else { // something went wrong, the email will most likely bounce } fclose($sock); }
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
By the way, some email domains will actually have other domains relaying emails in their behalf.
In order to validate the domain portion of the email, and find the correct host to connect to (the authoritative mail server for that domain) you need to make a DNS query of type MX.
see: http://www.php.net/manual/en/function.getmxrr.php
In order to validate the domain portion of the email, and find the correct host to connect to (the authoritative mail server for that domain) you need to make a DNS query of type MX.
see: http://www.php.net/manual/en/function.getmxrr.php
Last edited by digital-ether; Aug 24th, 2008 at 1:31 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
hi digitalethe,
i already tried getmxrr,
but i m really impresed with your reply and this url http://www.yuki-onna.co.uk/email/smtp.html is really great i wass now that something like this is possible in php but tring to find since 15 days and at last u give the answer.
Really so thankfull to u.
i already tried getmxrr,
but i m really impresed with your reply and this url http://www.yuki-onna.co.uk/email/smtp.html is really great i wass now that something like this is possible in php but tring to find since 15 days and at last u give the answer.
Really so thankfull to u.
Help as an alias
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
•
•
•
•
hi digitalethe,
i already tried getmxrr,
but i m really impresed with your reply and this url http://www.yuki-onna.co.uk/email/smtp.html is really great i wass now that something like this is possible in php but tring to find since 15 days and at last u give the answer.
Really so thankfull to u.
If needed on windows, one can replace the DNS functions with the Pear Net_DNS library.
http://pear.php.net/package/Net_DNS
php Syntax (Toggle Plain Text)
/** * Validate an Email Address Via SMTP * This queries the SMTP server to see if the email address is accepted. * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact * @author gabe@fijiwebdesign.com * @version 0.1a */ class SMTP_validateEmail { var $sock; var $user; var $domain; var $port = 25; var $max_conn_time = 30; var $max_read_time = 5; var $from_user = 'user'; var $from_domain = 'localhost'; var $debug = false; function SMTP_validateEmail($email = false, $sender = false) { if ($email) { $this->setEmail($email); } if ($sender) { $this->setSenderEmail($sender); } } function setEmail($email) { list($user, $domain) = explode('@', $email); $this->user = $user; $this->domain = $domain; } function setSenderEmail($email) { list($user, $domain) = explode('@', $email); $this->from_user = $user; $this->from_domain = $domain; } /** * Validate an Email Address * @param String $email Email to validate (recipient email) * @param String $sender Senders Email */ function validate($email = false, $sender = false) { if ($email) { $this->setEmail($email); } if ($sender) { $this->setSenderEmail($sender); } // retrieve SMTP Server via MX query on domain $result = getmxrr($this->domain, $hosts); // last fallback is the original domain array_push($hosts, $this->domain); $this->debug(print_r($hosts,1 )); $timeout = $this->max_conn_time/count($hosts); // try each host foreach($hosts as $host) { // connect to SMTP server $this->debug("try $host:$this->port\n"); if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) { socket_set_blocking($this->sock, false); break; } } // did we get a TCP socket if ($this->sock) { // say helo $this->send("HELO ".$this->from_domain); // tell of sender $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">"); // ask of recepient $reply = $this->send("RCPT TO: <".$this->user.'@'.$this->domain.">"); // quit $this->send("quit"); // close socket fclose($this->sock); // get code and msg from response list($code, $msg) = explode(' ', $reply); if ($code == '250') { // you received 250 so the email address was accepted return true; } } return false; } function send($msg) { fwrite($this->sock, $msg."\n"); $resp = ''; $start = $this->microtime_float(); while(1) { $reply = fread($this->sock, 2082); $resp .= $reply; if (($resp != '' && trim($reply) == '') || ($this->microtime_float() - $start > $this->max_read_time )) { break; } } $this->debug(">>>\n$msg\n"); $this->debug("<<<\n$resp"); return $resp; } /** * 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 $str; } } }
Example Usage:
php Syntax (Toggle Plain Text)
// the email to validate $email = 'joe@gmail.com'; // an optional sender $sender = 'user@example.com'; // instantiate the class $SMTP_Valid = new SMTP_validateEmail(); // turn debugging on to view the SMTP session $SMTP_Valid->debug = true; // do the validation $result = $SMTP_Valid->validate($email, $sender); // view results var_dump($result); echo $email.' is '.($result ? 'valid' : 'invalid')."\n"; // send email? if ($result) { //mail(...); }
I've added it to the code snippets:
http://www.daniweb.com/code/snippet940.html
Last edited by digital-ether; Aug 24th, 2008 at 3:29 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
k thnaks i will try
Help as an alias
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I don't see how you could get into any legal issues for connecting to an SMTP server to ask if an email exists, given that the email address you want to query is solicited and not spam?
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Is Stormpay a credible payment system? (eCommerce)
- Importing data from .csv then putting this on body of email (ASP)
- sending email (ASP.NET)
- PHP Mail with Sender ID (PHP)
- modify script (Perl)
Other Threads in the PHP Forum
- Previous Thread: convert PHP to javascript?
- Next Thread: combo box problem...
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error errorlog file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql popup query radio random recursion recursiveloop remote script search select server sessions snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube






