Ajay Krishna Dutta
Ajay Krishna Dutta

Reputation: 770

Check Email Id exist or not in PHP

I am trying to check whether a Email Id is Exist or not using php.I have checked many of examples,articles like https://code.google.com/archive/p/php-smtp-email-validation/ https://github.com/webdigi/SMTP-Based-Email-Validation/blob/master/checkemailexample.php. All are looks like the same.All are working perfect in case of gmail but do not work properly in the other mail service like yahoo etc etc. Is there any way to check the all email id before sending the mail?

Upvotes: 0

Views: 551

Answers (2)

Synchro
Synchro

Reputation: 37730

Connecting, issuing an RCPT TO and dropping the connection is very likely to get you blacklisted by the likes of Yahoo. The only reliable way is to go ahead and send the message you want to send. If it's successful, great, if it's not, you'd better have a bounce handler at the ready to receive the bounce, process it, and then you'll know for next time.

You can obviously eliminate some addresses beforehand by simple RFC validation, for example using PHP's filter_var function, as anything that fails that is very unlikely to deliver, you can also look up whether a domain exists and can receive mail at all - check both MX and A records exist in DNS - but beyond that you can't tell without really sending a message.

Upvotes: 0

Austin
Austin

Reputation: 1627

I have not found a good validation tool yet. Your best bet is to validate that the format is correct. Then to test it, send the user a message and ask them to perform some kind of action that links back to your site. You can easily use php and a database to record when the user interacts with your site. Then you will know for sure that the address is valid and is for the user you intended. This is also best practice to avoid being labeled as SPAM.

This is the best jQuery script I've found to validate the format:

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

Upvotes: 1

Related Questions