Monday, August 24, 2009

how to use Google's mail server to send email

Just a little over 2 years ago I enrolled in a 3 month course from Wesley Chun in Intermediate Python, held in Los Altos Hills on the main campus of Foothill College. I found Wesley's lesson plan to be very challenging, but in my journey as a computer programmer since, one of the nuggets of wisdom I come back to is his lesson on writing internet clients. One small task he prompted us with was a valuable exercise on how to write clients that use the servers run by free email providers (yahoo, aol, hotmail, gmail). First, a little history on the instruction leading up to the in-class assignment... Why, I remember like it was yesterday (swirling, squiggly lines as harp strings are plucked & played by unseen angels should now be occupying your visual and aural landscape):

Internet Client programming
All internet clients are built on top of TCP

We talked about four internet clients: ftp, nntp, pop3, smtp
After covering ftp and nntp, Wesley began with an overview of email, how electronic mail as a system is complex and to operate it requires lots of working pieces:
  • Message Transport Agent
    • responsible for moving email, routing, queueing, sending of email.
    • sendmail, postfix, qmail, exim (unix)
    • exchange (windows)
  • Message Transport System
    • protocol used by MTAs to transfer email host-to-host
    • Simple Mail Transfer Protocol
  • Message User Agent
    • protocol used to get email from servers client-to-host
    • post office protocols
    • internet message access protocols
At one time, every desk with a workstation had an email server, but this framework was not scalable. Wesley talked about Python's poplib, and about the SMTP interface in smtplib:
  1. connect
  2. login
  3. send email
  4. quit
then he challenged us to write our own POP and SMTP clients.

As we go to press, the code below, tailored specifically to work with Google's SMTP server, was tested to work on Windows 2000 & Vista, and Debian Linux 5.0 (Lenny):
def use_gmail_smtp(te, tffn, ttfs, subject, msg_body):
from smtplib import SMTP
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
SMTP_server = ''
username = 'your gmail handle'
passwd = 'your gmail password'
msg = MIMEMultipart()
msg['From'] = tffn
msg['To'] = ttfs
msg['Subject'] = subject
server = SMTP(SMTP_server, 587)
server.ehlo() # see note below for Python 2.5 and 2.6 users
ssl_connection_errors = server.starttls()
ehlo_connection_errors = server.ehlo()
server.login(username, passwd)
server.sendmail('tpc247', (te, ), msg.as_string())
A primer for the acronyms in the use_gmail_smtp() argument list:
  1. te is the target_email, the email address where you want the message to arrive
  2. tffn is the_from_field_name, what you want the user to see in the From: field's name portion. To tffn, Gmail will append <>
  3. ttfs is the_to_field_string, what you want the user to see in the To: field, usually in the format:
    "Your Name <>"
A previous incarnation of the code behaved correctly on Python 2.5, but strangely enough, not on Python 2.6, because it called:
ssl_connection_errors = server.starttls()
resulting in the error:
Traceback (most recent call last):
File "", line 11, in use_gmail_smtp
ssl_connection_errors = send_server.starttls()
File "C:\Python26\lib\", line 611, in starttls
SMTPException: STARTTLS extension not supported by server.
It's a mystery to me why this error is only on the aforementioned version of Python, but switching the call out was the answer.

I tested my script on 3 different operating systems, each running at least two different versions of Python:
  • Windows
    • 2000: Python 2.6.1, 2.6.2
    • Vista: Python 2.5.4, 2.6.1, 2.6.2
  • Debian Linux 5.0 Lenny: Python 2.5.2, 2.6.1, 2.6.2
Other notes: when installing Python 2.6.1 on Windows Vista, for the first time ever I saw:
Please wait while the installer finishes
determining your disk space requirements
and then the installation would stall and never complete. The workaround is to open a command prompt, and type:
msiexec.exe -package <mypackage.msi> –qr

For you who would like to use gmail to send emails that look like they come from your workplace, you can now do so without the "on behalf of" that make you look less than authentic.


No comments:

Post a Comment