Using MMS2R for mobile integration with Rails

Posted by Luke Francl
on Thursday, May 24

At Slantwise, we’ve been doing some projects that involve taking user-submitted content from cell phones. Using an MMS-to-email gateway, it’s straightforward to ingest photos and videos into a Rails application with ActionMailer. Every phone we’ve tested has the ability to send an MMS message to an email address, so this is a cheap and easy way to get started. We’d like to use a shortcode like Twitter’s 40404 some day, but they are hellaciously expensive.

While receiving the email is straightforward, you still have to deal with the advertising and general crap that is added to the messages by the phone carriers.

Here’s an example from Sprint, by far the worst carrier we’ve come across:

Sprint adds advertising and other garbage to your MMS message

Not only is this message stacked with ads and other nonsense, the worst part is that the photo isn’t actually included as an attachment! You have to download it from Sprint’s server.

Thankfully, we found the MMS2R library, created by Mike Mondragon. MMS2R greatly simplifies processing MMS messages. MMS2R removes advertising, eliminates default subjects, and makes fetching media from the message much easier. It even has a special case to download the real media for Sprint messages. MMS2R decodes and extracts files from multipart MIME email so you don’t have to!

Imagine you have a MediaItem model that has a title and a file associated with it (we used AttachmentFu to store the files). Here’s an ActionMailer you that will process an incoming MMS message and store it as a new MediaItem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'mms2r'
require 'mms2r/media'

class IncomingMmsHandler < ActionMailer::Base

  def receive(email)
    # create a new Media Item
    item = MediaItem.new
    
    begin
      # Parse the MMS attachments with MMS2R
      mms = MMS2R::Media.create(email)
      mms.process

      # Gets the subject, stripping out known carrier defaults
      item.title = mms.get_subject
    
      # Get the most likely media for the message
      # MMS2R mocks up a CGI.rb temp file object, 
      # so get_media can be used with AttachmentFu!
      item.uploaded_data = mms.get_media
    
      # persist the item
      item.save!
    ensure
      # clean up the temp files      
      mms.purge
    end
  end
end

The error handling in this example is rudimentary for simplicity’s sake.

This all works on a per-carrier basis. The currently supported carriers are:

  • AT&T/Cingular
  • Dobson/Cellular One
  • Nextel
  • Sprint
  • T-Mobile
  • Verizon

New rules have to be added to strip advertisements from unknown carriers (you’ll still be able to access the media). New carriers are easy to add, so don’t be shy about submitting a patch.

MMS2R is being used in production at several web sites including mymojobaby, Vedio.tv and our own project, which is still in private beta.

(This is a preview of some of what I’ll be speaking about at Ostrava on Rails. The speakers have just been posted and it looks like a great line up. Don’t miss it if you’re in Europe.)