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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#!/usr/bin/python """ This script checks if an email address exists on a mailserver. """ import logging import argparse from smtplib import SMTP MAILSERVER = "your.mailserver.com" PORT = 25 DEBUG_MODE = False RFC_LINK = r"https://goo.gl/9hYJQg" if __name__ == "__main__": PARSER = argparse.ArgumentParser( description="""DESCRIPTION: This script checks if an email address exists on a mailserver. """, usage="\n./emailchecker.py email@address \ \n./emailchecker.py -h \t\tfor help", PARSER.add_argument('emailaddress', help='email_address') PARSER.add_argument('--mailserver', required=False, default=MAILSERVER, help='mailserver address') PARSER.add_argument('--port', required=False, default=PORT, type=int, help='port number') USERSARGS = PARSER.parse_args() logging.basicConfig(level=logging.DEBUG if DEBUG_MODE else logging.ERROR, format='[%(asctime)s][%(levelname)s]: %(message)s') logging.info('Email checker script started') try: with SMTP(host=USERSARGS.mailserver, port=USERSARGS.port) as smtp: logging.info('Connecting to {}:{}'.format(USERSARGS.mailserver, USERSARGS.port)) result = smtp.verify(USERSARGS.emailaddress) print("SMTP CODE: {} \nMESSAGE: {}".format(*result)) logging.info('RFC1893, SMTP CODES: {}'.format(RFC_LINK)) logging.info('Connection closed ') except: logging.error("Failed to connect to {}:{}".format(USERSARGS.mailserver, USERSARGS.port)) |
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.