chris
chris

Reputation: 21237

Simple SMTP email validation function for php? Also, is it worth it?

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.

Upvotes: 6

Views: 8069

Answers (4)

Dominic Sayers
Dominic Sayers

Reputation: 1793

You are welcome to use my free PHP function is_email() to validate addresses. It's available here.

It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists.

You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.

Upvotes: 0

Webber
Webber

Reputation: 300

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/.

Upvotes: 2

yhager
yhager

Reputation: 1662

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.

Upvotes: 0

karim79
karim79

Reputation: 342795

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);

Upvotes: 4

Related Questions