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.

Checkcards message

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()
209:  smtp.login(gmailUser, gmailPassword)
210:  smtp.sendmail(mailFrom, mailTo, msg)

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.

If you’re wondering why I don’t use a service like Library Elf, the answer is simple: my library isn’t in the Elf system. Unless you happen to use the Naperville Public Library, my script won’t be of any direct use to you, but it’s on GitHub anyway, on the off-chance that other people can alter it to fit their own needs. If your family uses the library a lot, a centralized system for tracking everything you have checked out can make your life simpler.


  1. Sendmail is the venerable Unix utility for routing email over the network. It’s legendary for its cryptic configuration file and its vulnerabilities. It was Sendmail that allowed the Great Internet Worm of the late 80s to spread. Given how small the internet was back then, “great” is something of an overstatement, but that’s how it was thought of at the time. 

  2. On OS X, the sendmail command doesn’t really run Sendmail, it runs Postfix in Sendmail compatibility mode.