Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Created June 9, 2012 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdiggins/2899995 to your computer and use it in GitHub Desktop.
Save timdiggins/2899995 to your computer and use it in GitHub Desktop.
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] = 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment