Created
June 9, 2012 07:35
-
-
Save timdiggins/2899995 to your computer and use it in GitHub Desktop.
method to extract parts from a rails Mail::Message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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