Emailing Adobe Ideas to Posterous

In this article I will show you how I generated the following Posterous posts, the former coming from Adobe Ideas on my iPad and the latter coming straight from a PDF file.

Sketch-apply

Making Adobe Ideas Useful

One of my favorite apps for the iPad is Adobe Ideas.  I love being able to quickly pick up my iPad and sketch together an idea, whether it be a crude drawing, math formula, or software diagram, and show it to somebody else.  You can even email your sketches.

However, I was disappointed to find out that it only sends them as PDF's.  I want to email them as images so I can send them straight to Posterous.  I would then be able to sketch up something real quickly, and with the press of the button, make it available to the world instantly.  Talk about instant feedback!

It's also friendlier to email people images because a lot of email clients preview the image, and it's usually faster to open an image than a PDF. You lose some quality, but we're talking about rough sketches here.  

So I started thinking about how to do it.  I found out that ImageMagick has a "convert" utility which can convert PDF's to PNG's.  Perfect. All I have to do is create a fake email address like "convert@mydomain.com", and setup an email service on my server which, when receiving an email, grabs the attachments and converts them to images and re-sends them out.

It's not that easy, however.  Email is a pretty complicated beast.  Anyone who has setup and used qmail knows that.  I started installing qmail, but then I realized that I didn't want to go through that much headache and I don't even have an available domain to fiddle around with MX records.

My domain jlongster.com is setup with Google Apps, so I use gmail to manage its incoming email.  I realized that I should be able to download my emails using POP3 straight from gmail, kick off my service, and re-send the emails myself.  It turned out to work really well!

Here's what my sketch on posterous looks like.  This service turned out to be really neat since ImageMagick's "convert" utility translates each PDF page into one image.  That means that I can email full PDF's and suddenly Posterous' cool image gallery becomes a PDF previewer.  Check out a PDF preview in posterous here.

First, I set up a new user in Google Apps for jlongster.com.  This creates an email address where I send all my posts that need to be converted.

Then, I installed "fetchmail" on my server to download messages over POP3.  You have to use SSL with gmail, so I made sure I had all the right certificates installed with this article (honestly, see if it just works first).  "fetchmail" is pretty cool because it's just a barebones mail retrieval program designed to be hooked up to another program called an "MDA," or "Mail Delivery Agent."

I didn't need a complicated email setup; I just wanted my message.  I was hoping fetchmail simply sent the MDA the raw text of the email and it wasn't more complicated than that.  So I wrote an MDA which simply logs everything from standard input, and sure enough, I got the raw email with headers, text, and base64-encoded attachments.

That means I can write a Python script which simply creates a Message object from the standard input and go from there.  Long story short, fetchmail passes off the email to my custom MDA which is a Python script which parses the email, extracts the attachments, saves them to files, processes the files, and re-sends the email with the processed attachments.

You could do any processing you want, I simply just detect files of type "application/pdf" and run "convert" on them.

I setup fetchmail to run as a service every 90 seconds, and now I'm done!

Instructions

You can find the source code for my custom Python MDA here: 

You should run fetchmail with the "-m" argument and pass it the name of your python script.  Also, here is my fetchmail config file:

set postmaster "james"

poll pop.gmail.com with proto POP3
   user '<email>' with pass '<pass>'
   options ssl sslcertck sslcertpath '/usr/share/ssl/certs'

Feel free to ask me more questions in the comments.

Conclusion

The whole thing ended up being really neat and it was nice to brush up on these technologies.