Yoan Blanc’s weblog

Another lost swiss guy

June 2009

Email interface for Twitpic

Yoan BlancFri 26 June 2009, , , ,

The Lamson Project emerged recently, but dealing with emails as always been around. Sending emails can be a pain for other reasons than receiving email is one too. Let’s focus on the second one with a simple demo that posts a picture to TwitPic from an email using Twisted.

Disclaimer, I haven’t tested Lamson but feels more confident to go with a solution built on top of Twisted then another one redoing most of the job using plain asyncore and threads.

We will start by using txmailserver originally built by Duncan McGreggor, a set of tools to set up a basic STMP server, with POP3 support.

Read the file server.tac to see all the boiler plate, I’ll show the interesting parts.

def twitpic_it(dest, message):
 pass # see bellow

domains = {
 "twitpicit.org": [
  Script(r"[a-zA-Z_0-9\-\.]+\+.+",
         twitpic_it)
 ]
}

It’ll accept any email users that look like: me+mypassword@twitpicit.org and call the twitpic_it function. From there we can do whatever we want. No more details on the email treatment.

def twitpic_it(dest, message):
 username, password = dest.local.split("+", 1)
 
 # finding the part which is a picture
 # and building a temporary file with it
 file = file_from_message(message)

 if file is not None:
  datagen, headers = multipart_encode({
   "username": username,
   "password": password,
   "message": message["Subject"],
   "media": file
  })

  request = urllib2.Request(
   "http://twitpic.com/api/uploadAndPost",
   "".join(datagen),
   headers)

  file.close()

  urllib2.urlopen(request).read()

There you go, you’re able to twitpic everything you like from any email client. After you’ve set up that on one of your domain of course or you can try it locally using a simplistic Python script: test.py.

$ twistd -ny server.tac

in a different shell:

$ python test.py mypicture.png My message

The server should display the response from Twitpic. Of course this is purely overkill if you can directly use the API.

Email is still an interesting interface for people that aren’t techie at all. For people that sits behind a desk with only that as an interface to the outside world because the IT service decided that you shouldn’t spend your day playing on Facebook or because you are Richard M. Stallman (RMS) himself.

Most of the time a bad usage of emails is made, like answering leads to nothing (the infamous noreply), of you have to visit it inside a browser to do something. Don’t neglect it as a tool that most of the people understand and know how to use it. It means they don’t have to think too much in order to use it.

The code is on Launchpad.

Email premier usage popularisant le web reste une bête difficile à dompter, en raison de son usage publicitaire intensif, mais a le potentiel d’être un outil de choix pour la simple et bonne raison qu’il est connu de toutes et tous et n’est que très rarement bloqué par un service informatique faisant du zèle (pour ceux qui intéragissent avec le web essentiellement depuis leur place de travail).

En exemple un mini serveur SMTP ne fonctionnant qu’en mode réception — rien ne vaudra jamais un véritable MTA pour fonctionner en mode relay (émission), type postfix ­— qui va poster l’image sur Twitpic avec le texte donné en titre.

Le code est écrit en Python et repose sur Twisted, un ensemble d’outil permettant de programmer de manière évènementielle et contenant bon nombre de protocoles supportés (tel SMTP et POP3, dans notre cas). Le code de base provient du projet txmailserver qui a pour but d’offrir un serveur SMTP/POP3 minimaliste.

Comme vous pouvez le voir dans ce script fonctionnant comme serveur permet de passer les adresses reçues à une fonction. Ensuite, libre à votre imagination créative d’y faire ce que bon vous semble. Je ne connais pas Lamson qui a pour but de servir de boîte à outils pour ce type d’applications là.

Trouvez le code sur Launchpad.

About

meYoan Blanc is a web developer that lives in Norway (Markveien 24, 0554 Oslo) works for Opera and comes from La Chaux-de-Fonds. This web site is for this weblog, a playground for random stuffs and can help keeping me connected with some friends out there.

Get my vCard or contact me by phone (skype:yoan.blanc) or email ().

Misc

RSS, list.blogug.ch

This site reflects only my opinion and is not affiliated with anyone else.

copyright 2006-2009 — doSimple.ch