Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
method to extract parts from a rails Mail::Message
def parts(email)
splits = email.encoded.split(/(\r\n)*----==_mimepart_[a-zA-Z0-9_]+(\r\n)*/)
parts = {header: splits[0]}
splits[1..-2].each do |part|
headers, body = part.split(/\r\n\r\n/, 2)
if(m = /Content-Type: ([^; ]+);?\s/.match(headers))
type = m[1]
parts[type] =, body:body)
return parts
#example of using this within rspec
it "should have content" do
email = ActionMailer::Base.deliveries.last
email.should_not be_nil
parts = parts(email)
parts.keys.should include('text/html')
parts.keys.should include('text/plain')
parts["text/plain"].body.should match(something)
parts["text/html"].body.should match(something_else)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment