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(STDIN.read)" rebuid alias table sendmail –bi Virtual alias file: mms@site.commms@site.com : mms_mail Now all email send to mms@site.com gets forwarded to AccountMailer.receive()mms@site.com

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 –John_doe_1212@site.comJohn_doe_1212@site.com –all@site.comall@site.com 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 number@carrier.comnumber@carrier.com Ask users for their mobile number on site Give users a verification code to send from their mobile device –send all@site.com 555555 to verify your accountall@site.com 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 InvalidEmailException.new("Bunk email") elsif !user.is_bonded? #look for verification code only plainparts = [] email_text.parts.each 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 user.save! 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 mms.media['image/jpeg'][0] asset = Asset.new(mms.media['image/jpeg]) asset.save end rescue MobileBondException => e puts e end Full process in Receive function 1)Parse email for who it is from 12345678912@verizon.net 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 mms.media['text/plain'] mms.media[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, user.media.size 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