Lukas Klein

Python and Django developer. Docker enthusiast. This is my blog.

Checking if an email address exists

Today I was faced with an interesting question: How can you tell if an email-address exists? Yep, not if it’s valid (that’s easy with some regexp-magic), but if it actually exists. I came up with a simple Python-solution.

What if we want to check, if, for example, [email protected], exists? It’s a valid email address, but it obviously doesn’t belong to any account. My solution uses SMTP, the protocol that is used to send emails.

At first we have to find the MX-record of the domain - it tells you what mail server you have to use. I’m using the Python DNS Library to get the actual records and smtplib to try to establish a connection.

I’m maybe a bit lazy, but instead of reading all the smtplib documentation I decided to implement the SMTP protocol on my own. We basically only need to go as far as to the RCPT TO command, to which the server replies with an 550 error if the recipient does not exist.

I’m maybe bad at describing things, but I hope you can find my code helpful for whatever you’re planning. But please be nice and don’t use it for spamming :)