Check if a mailbox exists with Python and smtplib

Every day we discover different tools that should make our life easier, enchanted by the promise of achieving the best result without any effort. If we can master that tool or technique we can solve many problems even without understanding or finding the root cause, right?

So often I’m guilty of making that assumption, because it not just lazyness it’s harder and it takes time and patience to break complicated puzzles into small simple pieces. Even more to put it all those parts back together to finally find answers to questions like: How? When? Why?

Working across different integrated systems I often find an application that sends some email notifications and some messages are bounced with a smtp error like a specific mailbox was not found.

So to break the habit using a bigger software solutions and going through mail logs, application logs… I wrote a simple application to connects to the mail server using smtp and checks if the error is a 200 (mailbox exists) or 500 (mailbox not found).

Using Python and a smtplib library is way more interesting, especially now that I don’t write Python code every day.

In short once you edit the mail server name and the port number, this script will check if the email address passed as argument exists on that server or not.

It is a beautiful excuse for me to remove some dust.. and reading the RFC for the SMTP protocol, avoiding a lot of different web tool or opening a telnet client.

But in the end… reading the logs is always inevitable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.