Posting to Drupal from an iPhone via SMS

Why?

Why do I want to do this? The iPhone allows you to send regular emails, and the MailHandler Drupal module already allows you to submit content via email. This is about 90% of what I want. I'm going to be driving cross-country, and in many places I'll have phone reception but no data service. SMS requires only normal cell reception, not Edge/3G/etc data coverage, so this is pretty important for me.

Why don't I just use Twitter? That's harder to answer. Mainly, DIY is more fun.

So, all objections being skillfully silenced, here's what I did...

Getting Started with Drupal's MailHandler Module

First, read the Drupal HOWTO Submit Blog post by email. That article assumes MailHandler is already installed. This wasn't true for me so I did that first. You can get MailHandler from its project page. If you don't know how to do this, try this guide on install a Drupal module.

You will also need the PHP IMAP extension. On an Ubuntu/Debian server, this was as easy as apt-get install php5-imap.

MailHandler Configuration and Debugging

It's very useful to see mail server logs when you try to check mail for the first time. In my case, I'd call it essential, since I have no idea how I would have solved a few issues without access to those logs.

My imap server is open on port 993 (SSL/TLS) only. I hit some errors due to the server requiring SSLv3.

May 10 09:06:20 new couriertcpd: couriertls: connect: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

I changed the IMAP server configuration to allow to SSLv2, and was able to connect from Drupal to check for new messages. I hope to find a better solution, so I can go back to requiring SSLv3. Please leave a comment if you have any more information about this.

I used the following commands to classify my mobile posts (more on those below). I found a page which provides more information on MailHandler commands.

# create a story
 type: story
# mark the story as published
 status: 1
# allow comments
 comment: 2
# In taxonomy 3 (access control), classify this post as visible to a logged-in user
 taxonomy[3]: [LoggedIn]
# In taxonomy 1 (free tagging), add the tag 'mobile update'
 taxonomy[1]: [mobile update]

If you use MailHandler commands to classify your posts into taxonomies (categories), you need to enter the string names of your taxonomy terms, not the ids. On our site, we use a taxonomy to control content access via the Taxonomy Access Control module. I had used 'taxonomy[1]:[15]', which was wrong. 'taxonomy[3]:[LoggedIn]' was what i wanted. Not good if you ever edit your taxonomy terms, but it's good enough for me right now.

The Part About the iPhone

The iPhone SMS interface doesn't let you compose a text message to an email address. I guess they're assuming you'll just send real emails if you want to email someone, but as stated above that doesn't work for me. I worked around this as follows:

  • I added the email address that MailHandler checks to my email client (Mail.app, though Thunderbird or Outlook would work fine.)
  • I then sent email from this address to my iPhone's SMS gateway address. This looks something like 4809991212@txt.att.net. It'll vary by cell provider, but all the major carriers offer this service.
  • This email arrived on the iPhone as a text message. When I 'reply' to it, I'm sending an SMS which is converted to an email and delivered.
  • MailHandler will only accept messages from a recognized user. You'll need to create a Drupal account which has your cell phone's address as its email. I created a user 'mobile-alex' for this. This is a hack around the fact that Drupal doesn't support multiple emails per user. I suggest creating this user directly in the Drupal admin interface, since normal user registration usually requires you to reply to a verification email or visit a specific URL, which will be a PITA from a text message.

And now, finally, I can post Drupal updates via SMS.

Drawbacks and Rough Edges

The subject of the text message, which becomes the subject of your post on your website, is always 'RE:' plus your original subject. I'm not sure if there's a workaround for this in the Mailhandler. A command which let you specify a subject would be useful, but I doubt that's available. MailHandler was designed for regular email, which has it's own subject field without hackery.

If you clear your list of SMS conversations, you won't be able to post from your phone until you send yourself another email from your Druapal address to your phone. This is annoying. I'm going to keep poking around to see if there's some way to compose an SMS to an email address without this hack.

You'll need to configure Mailhandler to use '-----Original Message-----' as your signature. This, plus the original email, is appended to the SMS you send from the phone. Telling MailHandler this is a signature means it gets removed from the final post.

Conclusion

So, this is rough, and could use improvement. Please leave me comments if you have ideas on how to improve this setup. I was able to do this in a few hours, and it's allowing me to do just what I was hoping. Maybe others will get some use out of it as well.

Update

There seems to be some maximum age for messages on the AT&T email to SMS gateway. After about two weeks, I was unable to send a reply SMS. I immediately received an error SMS stating that the message I was trying to reply to was unknown. I composed a new email to myself (delivered as SMS) and this jury-rigged monster resumed functioning as advertised. Unfortunately, this happened at about mile 50 of my 1500 mile drive, so I had no posts from the road. Oh well. Immediacy is vastly overrated. (Which is another answer to 'Why not Twitter?')