i would like to develop application to get email address, and i need to send confirmation to that email id, before that i need to check whether the entered email id is valid or not. Is there any possibilities to check using php or jsp?
-
why did you tag the question "jsp"? are you using both jsp and php or you mean js (javascript)? – mishu Dec 06 '11 at 07:58
-
Isn't this question a duplicate of http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses ? – codeling Dec 06 '11 at 08:03
-
@nyarlathotep it seems that the other question is focused on regex and this one on any other method.. – mishu Dec 06 '11 at 08:04
-
to know both technologies, by using java and also by using php – Praveen Jayakrishnakumar Dec 06 '11 at 08:22
5 Answers
if by validate you mean checking if the string can be an email address you can use this php line
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
and after that if the variable is not empty, the email address is valid (check http://php.net/filter_var)
if by validate you meant checking if it exists - you can't

- 5,347
- 1
- 21
- 39
-
right, and the filter module enabled; it is enabled by default after 5.2, but I was just saying for the record :) – mishu Dec 06 '11 at 08:00
You don't understand the matter.
The very purpose of sending confirmation email is to check if such email exists, valid and read by the user.
Do not reinvent the wheel and do not listen to local "theorists" who never used the tools they are peddling here.
Sending the confirmation email is the only reliable way to tell if such an account exists.

- 156,878
- 40
- 214
- 345
-
Huh? The OP asked for a way to check whether the entered email is valid or not, a few of our solutions are hardly 'peddled tools'. – Mike Purcell Dec 06 '11 at 21:49
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") ||
↪checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
Thanks to http://www.linuxjournal.com/article/9585

- 8,979
- 8
- 47
- 65
-
thanks for your response, but i would like to check whether the enter email id is genuine? eg. if suppose i am giving email as xyz@gmail.com, i need to check and send acknowledgement to this particular email id before that i find entered email id is correct domain with already created account. any possibilities? – Praveen Jayakrishnakumar Dec 06 '11 at 07:53
-
This one checks the DNS records if the site after the `@` exists - that's pretty much the best you can do. – Andreas Eriksson Dec 06 '11 at 07:54
-
@Praveen the only way to find out whether an E-Mail address is genuine is to send an E-Mail to it. That's the whole point of sending a confirmation E-Mail – Pekka Dec 06 '11 at 22:55
No reason to reinvent the wheel, Zend Framework offers a email validation library, check out the docs for more info.
Looks like you can do:
// Under EmailAddress section of docs
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email)) {
// email appears to be valid
} else {
// email is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}

- 19,847
- 10
- 52
- 89
Checking if a mail exist over SMTP with PHP
http://www.webdigi.co.uk/blog/wp-content/uploads/2009/01/smtpvalidateclassphp.txt
example
<?php
// the email to validate
$email = 'joe@gmail.com';
// an optional sender
$sender = 'user@example.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";
// send email?
if ($result) {
//mail(...);
}
?>

- 463
- 1
- 7
- 18
-
this is a php validation via SMTP to see if a email real exist I do not know any jsp ... sorry – Stelios Joseph Karras Dec 06 '11 at 08:39
-
Hi thanks for your response, but its shows me warning as Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\1.php on line 90 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\1.php on line 127 array(0) { } joe@gmail.com is invalid – Praveen Jayakrishnakumar Dec 06 '11 at 08:55