Created
June 24, 2010 13:24
-
-
Save djgraham/451438 to your computer and use it in GitHub Desktop.
Parsing multipart message bodies with Tmail in ruby
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
# the usual disclaimers apply, this may or may not work for you, and it's rather hacky, but works for me(tm) :) | |
# first parse the mail | |
mail = TMail::Mail.parse(params[:email]) | |
# parse the mail (as below) | |
email.body = Email.get_multipart_body(mail) | |
# in your model... | |
# attempt to grab the body of the message - if not a multipart message return the body alone | |
def self.get_multipart_body(mail) | |
types = mail.parts.collect(&:content_type) | |
if types.include?("multipart/alternative") | |
m_part = mail.parts.find { | |
|m| m.content_type == "multipart/alternative" | |
} | |
sub_types = m_part.parts.collect(&:content_type) | |
if sub_types.include?("text/plain") | |
m_part2 = m_part.parts.find { | |
|m| m.content_type == "text/plain" | |
} | |
ret = m_part2.body | |
else | |
ret = m_part # get_text_html(m_part) | |
end | |
elsif types.include?("text/plain") | |
m_part = mail.parts.find { | |
|m| m.content_type == "text/plain" | |
} | |
ret = m_part.body # get_text_plain(m_part) | |
else | |
ret = mail.body | |
end | |
ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment