cat /dev/brain > blog

yet another weblog – by Sven A. Schmidt

iPhone SMS to Mail Synchronization

leave a comment »

Don’t you wish you could access your iphone text messages with I know I do. It would be so convenient to be able to search your text messages with spotlight and you could even switch a discussion to mail and back and still have a complete overview in one place.

My initial hope to get this was to write an iphone app that forwards text messages to your own mail account. Unfortunately, the sms database is not accessible from other apps and there’s no sms API (yet).

However, I remembered seeing scripts that accessed the sms database file that’s backed up to your Mac via iTunes. It turns out it’s quite simple to take this database and synchronize it with your IMAP account via a python script. This process can potentially even be semi-automatic if this script is set up as a folder action on the MobileSync backup folder. But that’s for later, here’s what the script does:

It takes your text messages from your backup and saves them in a (configurable) mailbox on your IMAP account. The subject line is generated in a way that will allow’s threading to group the messages.

Here’s what the uploaded sms database looks like:

Download the script here:

In order to run it, set up a config file named “.sms2mail.conf” (without the quotes and mind the dot) in your home directory with the following content:

user = email_account_username
password = email_account_password
sms_mailbox = sms
host =

mynumber = ‘+49123456789’

Replace the values with your IMAP connection details, the name of the mailbox you want to store the messages in (you need to create this mailbox before you run the script) and finally your phone number. This doesn’t actually have to be your number, you can also use your name. It’s used to fill the To or From email header for incoming and outgoing text messages. This information is not stored in the sms database and therefore has to be configured.

Once this is setup, simply run the script:


The script does not require any additional python packages on OSX 10.5 and should run out-of-the-box.

How it works:

  1. Scans your iphone backup files for your sms database
  2. Pulls all text messages from the database
  3. Creates an email message for each one with a unique hash generated from a number of message fields
  4. Checks the configured IMAP mailbox for existing sms emails and downloads their ids
  5. Every message with a new id is uploaded to the mailbox. This ensures that subsequent runs will only upload new messages. Note that messages you delete in your mailbox will be uploaded again on next sync unless you also delete the original text message.

Things to do (and here I’m especially grateful for input):

  • Describe how to set up a folder action that will trigger the script automatically when an iTunes backup has completed. This might be a bit tricky, because I imagine the folder action will trigger prematurely when the backup starts. One might have to monitor the exact sms database file directly, which is needlessly complicated, because the backup filenames are pretty obscure.
  • Figure out how to upload unicode messages. I haven’t figured out how to do this yet, I’m simply using the “replace” option on the ascii encode for the message body. This is not ideal, because it replaces anything not ascii with a question mark. I guess one would have to create a mime encoded message body (base64?) but I don’t know how to do that yet.
  • Improve IMAP access efficiency. Currently the script does an IMAP fetch of all messages in the sms mailbox to find the ids already present. I’m pretty sure this could be improved a lot by only fetching the headers or even only the ‘Sms-Id’ header. But I have no clue what fetch/select statement one would have to use. I’m not really tempted to read the IMAP RFC either at this point… For what it’s worth this script works fine in reasonable time as it is right now. The initial upload of 500 messages takes 7 minutes over DSL. Subsequent syncs take 2.5 minutes. I can live with that for now but it remains to be seen how well this will scale to a few thousand messages. It might be an idea to cache the uploaded ids locally. That would also fix the re-upload on delete.
  • Maybe add some more feedback printout when there are no new uploads. Right now, the script only prints the IMAP server’s response for each uploaded message and otherwise keeps quiet.
  • Integrate the script with Address Book (the API is usable from python) and use people’s names in the To and From fields instead of their phone numbers.

Written by sas

August 31, 2008 at 15:08

Posted in iphone

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s