Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Bonding Accepting and parsing MMS messages.

Similar presentations

Presentation on theme: "Mobile Bonding Accepting and parsing MMS messages."— Presentation transcript:

1 Mobile Bonding Accepting and parsing MMS messages

2 problems that need to be solved How to… – Pull mails into application –Parse emails –Associate email to a user account –Decode attachments –Save to database

3 Pulling emails into application Initial thought –run a cron job on mailbox every few minutes and parse new emails, but very hackish and not instant. How we ended up doing it –setup MTA to forward email to your applications method for handling emails ActionMailer.recieve(raw_email)

4 Setting up MTA to forward emails were using Exim – create a new alias in /etc/aliases mms_mail: "| /path/to/ruby /path/to/app/script/runner 'AccountMailer.receive(" rebuid alias table sendmail –bi Virtual alias file: : mms_mail Now all email send to gets forwarded to AccountMailer.receive()

5 Parsing emails Go figure, rails provides a great built in method for this class MyMailer < ActionMailer::Base def receive(mail)... end Put in a raw email, returns a workable object (TMAIL)

6 class AccountMailer < ActionMailer::Base def receive(email) puts email.title if email.has_attachments? puts email.attachments.count end >> check out my car >> 3

7 Associating emails with a user account Users need to know where to send emails – – Site needs to know who sent an email –Was it john doe, or a spam bot? Emails need to be verified

8 Verifying Emails / Users Emails contain FROM: which is in the format of Ask users for their mobile number on site Give users a verification code to send from their mobile device –send 555555 to verify your Site matches number / code combination => verifies users mobile device

9 def receive(email_text) user = User.find_user_from_mms(email_text) if !user raise"Bunk email") elsif !user.is_bonded? #look for verification code only plainparts = [] do |part| plainparts << part if part.content_type == "text/plain" code_correct = user.verification_code_correct?(plainparts.body) end if code_correct user.bonded = true begin! AccountMailer.deliver_bond_confirmation(user, tmail.from[0]) rescue Exception => e puts "error saving new user bond" end else AccountMailer.deliver_bond_reminder(user) end elsif user.is_bonded? #user is found and bonded, return user, asset, post mms = MMS2R::Media.create(email_text) mms.process begin if['image/jpeg'][0] asset =['image/jpeg]) end rescue MobileBondException => e puts e end Full process in Receive function 1)Parse email for who it is from 2)Match mobile number to user 3)If user is not bonded, look for verification code 4)If user is bonded, parse email

10 Decoding Attachments MMS messages are full of junk Plugin MMS2R handles removing junk by carrier (verizon puts in 6 images) mms = MMS2R::Media.create(email_text) mms.process['text/plain'][image/jpeg']

11 Testing Cant setup local email server, so fake it class AccountMailerTest < Test::Unit::TestCase … def test_should_receive_video_from_email AccountMailer.receive(raw_email("mms-video.mail")) assert_equal 1, End Private def raw_email(action) IO.readlines("#{FIXTURES_PATH}/Mail/#{action}").join End end

Download ppt "Mobile Bonding Accepting and parsing MMS messages."

Similar presentations

Ads by Google