tags:

views:

442

answers:

3

Does anybody have a good function for validating email addresses by SMTP in PHP? Also, is it worth it? Will it slow down my server?

--> EDIT: I am referring to something like this:

http://onwebdevelopment.blogspot.com/2008/08/php-email-address-validation-through.html

which is meant to complement validation of the syntax of the email address.

It looks complicated though, and I was hoping there was a simpler way of doing this.

+1  A: 

If you want to check if there is a mail exchanger at the domain, you can use something like this:

/*checks if email is well formed and optionally the existence of a MX at that domain*/
function checkEmail($email, $domainCheck = false)
{
 if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'.
       '\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) {
  if ($domainCheck && function_exists('checkdnsrr')) {
   list (, $domain)  = explode('@', $email);
   if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
    return true;
   }
   return false;
  }
  return true;
 }
 return false;
}

Usage:

$validated = checkEmail('[email protected]', true);
karim79
A: 

Here's such a code, taken from the drupal module email_verify. There are a couple of Drupal specific calls there, but it should not take a lot of time to clean it up for a generic PHP function:

Also note that some web hosts block outgoing port 25, as it is mostly used by spammers. If your host is practicing such a block, you will not be able to use this form of verification.

yhager
+1  A: 

Here is what I believe you are looking for. It does a validation with the SMTP server. It shows PHP code. http://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-address-exists-without-sending-an-email/.

Webber
thanks that looks interesting
chris