public
Created

method to extract parts from a rails Mail::Message

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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] = OpenStruct.new(headers:headers, body:body)
end
end
return parts
end
 
 
#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)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.