# Getting around a GMail restriction

For several years now, I’ve had a script called checkcards that logs into my local library, collects information on the items my family has checked out and on hold, and sends me an HTML email with the information presented in tabular form.

I have launchd set up on my office iMac to run this script every morning. My wife also gets a copy through a mail rule on the server.

Until recently, this message was generated through a Python script using the mechanize library and then sent from my iMac via the sendmail command. 12 In late July, I noticed that the email from checkcards had stopped. I’m no email routing expert, but it appeared to me that GMail was refusing to deliver messages routed to it from my computer.

This wasn’t a big surprise. If anything, it was more surprising that GMail had ever accepted messages routed to it from an unauthenticated machine like my iMac. Even back in the 90s, the onslaught of spam had caused mail servers to become more restrictive on who they’d route mail for. I remember having to add special settings to the Exim configuration on my Linux box to get around these restrictions.

With sendmail no longer working, I needed a new way to get my messages delivered. I decided to try out Python’s smtplib module, and after a bit of experimenting I found I could send my messages with just a few lines tacked onto the end of the script. Here are the lines:

python:
206:  # Send the message through GMail.
207:  smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
208:  smtp.ehlo()

Line 207 establishes the connection to the GMail SMTP server on port 465. Line 208 starts the SMTP communication with an EHLO (extended hello) command. Line 209 then logs on using my GMail credentials (established earlier in the script), and Line 210 actually sends the message. It was much simpler than I feared.
2. On OS X, the sendmail command doesn’t really run Sendmail, it runs Postfix in Sendmail compatibility mode.