Postmark Mitt
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.
Install
gem install postmark-mitt
Example
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 # "[email protected]"
email.from # Bob Bobson <[email protected]>
email.from_email # [email protected]
email.text_body
email.html_body
email.headers # returns a hash of the headers
email. # array of attachment objects
= email..first
.file_name
.content_type
.read # will base64 decode the content... eventually this will lazy-load the file from Postmark
.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)
= User.find_by_api_email(mitt.to)
# send an email back saying we couldn't find them
post = new
post.title = mitt.subject
post. =
post.photo = mitt..first.read
post. = mitt. # Make sure we don't process the same email twice
# You get the idea, right?
post.save
post
end
end
Note on Patches/Pull Requests
-
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.
Authors & Contributors
-
Randy Schmidt
Copyright
Copyright © 2011 Randy Schmidt. See LICENSE for details.