How to check if an email ID is real without sending email
Introduction (Check if an email ID is real):
We might encounter a situation many times wherein we would like to check if an email ID is real or fake. We would be interested to ascertain that without sending an email to that ID. It’s possible to do that programmatically and this blog will tell you how ?
To check if an email exists or not, a fairly simple process is to be followed as below
1) Extract domain name and user from the email ID. For eg firstname.lastname@example.org will equate to user: abc and domain xyz.com.
2) Extract MX records (mail exchanger records) corresponding to the domain using standard APIs of the programming language that you choose to use. In this blog, its PHP. But one can choose to use Python,Perl,Ruby or any other language of their choice. Mail exchanger records signify the domain responsible for accepting emails from senders.
3) Set a certain connection timeout while trying to connect to the domain through TCP socket.
4) Open a socket to the designated MX domain using socket APIs.
5) If connection is refused, straight away it’s concluded that the domain isn’t accepting emails and thus the email ID is fake.
6) If connection is established , the next task is to verify if mailbox exists for the user specified by the email ID. For this, open socket connection to the SMTP server , first by sending MAIL FROM command followed by RCPT TO command. If the status code returned by RCPT TO command is either 250, 450,451 or 452, the mail exists and it’s real. If any other status code is returned, that means, mail doesn’t exist.
Below is a small TABLE indicating different status codes with their meanings
|200||(nonstandard success response, see rfc876)|
|211||System status, or system help reply|
|220||<domain> Service ready|
|221||<domain> Service closing transmission channel|
|250||Requested mail action okay, completed|
|251||User not local; will forward to <forward-path>|
|252||Cannot VRFY user, but will accept message and attempt delivery|
|354||Start mail input; end with <CRLF>.<CRLF>|
|421||<domain> Service not available, closing transmission channel|
|450||Requested mail action not taken: mailbox unavailable|
|451||Requested action aborted: local error in processing|
|452||Requested action not taken: insufficient system storage|
|500||Syntax error, command unrecognised|
|501||Syntax error in parameters or arguments|
|502||Command not implemented|
|503||Bad sequence of commands|
|504||Command parameter not implemented|
|521||<domain> does not accept mail (see rfc1846)|
|530||Access denied (???a Sendmailism)|
|550||Requested action not taken: mailbox unavailable|
|551||User not local; please try <forward-path>|
|552||Requested mail action aborted: exceeded storage allocation|
|553||Requested action not taken: mailbox name not allowed|
Demo below to see all this in action:-
Below is the end to end code to achieve the above :-
Client code (HTML)
Server Side code (PHP)
Verify email class PHP (Third party)
The above is just a simple demo using PHP. But the same can be implemented using Python,Ruby or any other language of your choice.