Convert text/plain content out of e-mail to normal text
When i try to read some email from my IMAP account:
imap.search(['NOT','SEEN']).each do |message_id|
mail = imap.fetc开发者_高级运维h(message_id, "BODY[1]")
end
i get all kinds of ascii?? codes in my string, like =20 =93 =94 etc. Tried already lots of things to convert or decode, but no success. How can i get rid of these codes?
There are a number of different options for encoding the message body such as quoted-printable, base-64 and so on. The easiest thing to do in Ruby is to pass the whole message into the mail gem, let it do the parsing and then output the plain text content.
message = Mail.new(raw_source)
puts message.body.decoded
In experience you might actually find that you need to do something like the following:
message.multipart? ? (message.text_part ? message.text_part.body.decoded : nil) : message.body.decoded
We use something similar to this when we send the message to an app in CloudMailin in order to make sure we find the plain part as it's not always guaranteed that the plain part will be in the body and not mime encoded.
I had to use something like the following to parse out the email.
text = message.multipart? ? (message.text_part ? message.text_part.body.decoded : message.html_part.body.decoded) : message.body.decoded
One of the messages I parsed did not contain text_part at all, so putting a nil in there won't work in that case.
Sounds like you have found a Quoted-printable body. You should look up what the encoding is for the body, and parse it accordingly. Seems like Net::IMAP::BodyTypeBasic can give you this information, but I'm afraid I don't know enough ruby to get you any further.
精彩评论