Test SMTP AUTH with Python

I needed to be able to validate a service providing SMTP AUTH was working properly. Google landed me a few suggestions, the best being ASMTPTest from GMTA.

I too wanted similar functionality, but so that my unit test could be loaded and ran at any time without human intervention (for this particular test). In either case, I began going down the path of sub-classing Python’s smtplib.SMTP class. But reading through the standard python class, some time later, I finally saw that SMTP AUTH is already supported.

I was originally looking for .auth for authenticate, when quite simply the .login method is actually what I needed all along! 🙂

Threw away my new class and begin using SMTP AUTH from the standard python libraries:
import smtplib
srv = smtplib.SMTP('<<server host name>>')
srv.login('<<username>>', '<<password>>')
srv.helo('<<originating server host name/ip>>')
srv.sendmail(<<from email>>, <<to email>>, <<email msg>>)
srv.quit()

Hope this helps the next Googler…

The small print:

1) I did not compare the differences between ASMTPTest and libsmtp yet. If you do before I get around to it, let me know.

2) There may be other libraries out there like Twisted. But I opted to stick with standard, core, basic libraries that come with a language where ever possible. This makes my unit test suite very portable without having to “set up the dependencies”.

Advertisements

6 responses to “Test SMTP AUTH with Python

  1. for gmail you will have to add starttls() and ehlo().

    server = smtplib.SMTP(‘smtp.gmail.com’)
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(“xxx@gmail.com”,”xxx”)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()

  2. Just a note, you will most likely need to specify a port:

    server = smtplib.SMTP(’smtp.gmail.com’, 587)

    cheers.

  3. I tested both Benegal and Andrew’s versions for gmail. Both worked correctly with gmail.

    Thanks guys…very helpful!

  4. Got a error in the server.login line

    File “C:\programme\Python30\lib\smtplib.py”, line 580, in login
    AUTH_PLAIN + ” ” + encode_plain(user, password))
    File “C:\programme\Python30\lib\smtplib.py”, line 545, in encode_plain
    return encode_base64(“%s%s” % (user, password))
    File “C:\programme\Python30\lib\email\base64mime.py”, line 96, in body_encode
    enc = b2a_base64(s[i:i + max_unencoded]).decode(“ascii”)
    TypeError: b2a_base64() argument 1 must be bytes or buffer, not str

  5. chill, check the
    http://bugs.python.org/issue5259
    you need to fix smtplib.py

  6. how to make it work with hotmail?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s