WARNING: ABANDONWARE-ish USE WITH CAUTION
This gem provides a little wrapper object for taking JSON from Postmark’s incoming email system and composing it back into an object resembling an email. This comes in handy when you are trying to figure out what to do with the email.
Postmark-mitt has been verified to work on Ruby Enterprise Edition 1.8.7, 1.9.2 and 1.9.3.
gem install postmark-mitt
require 'lib/postmark_mitt' # if you are just playing with this library email = Postmark::Mitt.new(File.read("path/to/json/file/in/spec/fixtures")) # if you are doing this in a controller email = Postmark::Mitt.new(request.body.read) email.to # "api-hash@inbound.postmarkapp.com" email.from # Bob Bobson <bob@bob.com> email.from_email # bob@bob.com email.text_body email.html_body email.headers # returns a hash of the headers email.attachments # array of attachment objects attachment = email.attachments.first attachment.file_name attachment.content_type attachment.read # will base64 decode the content... eventually this will lazy-load the file from Postmark attachment.size # NYI # You get the idea
A lot of times you want to do something with the email, like create a post
mitt = Postmark::Mitt.new(request.body.read) Post.create_from_postmark(mitt) class Post def self.create_from_postmark(mitt) author = User.find_by_api_email(mitt.to) handle_no_author # send an email back saying we couldn't find them post = new post.title = mitt.subject post.author = author post.photo = mitt.attachments.first.read post.message_id = mitt.message_id # Make sure we don't process the same email twice # You get the idea, right? post.save post end end
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history.
-
Send me a pull request. Bonus points for topic branches.
-
Randy Schmidt
Copyright © 2011 Randy Schmidt. See LICENSE for details.