Martijn Bakker
Martijn Bakker

Reputation: 148

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.fetch(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?

Upvotes: 2

Views: 1139

Answers (3)

zacropetricopus
zacropetricopus

Reputation: 407

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.

Upvotes: 1

Steve Smith
Steve Smith

Reputation: 5201

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.

Upvotes: 4

Anders Lindahl
Anders Lindahl

Reputation: 42870

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.

Upvotes: 0

Related Questions