0
votes

I am using Ruby's Net::IMAP lib to fetch mail and am using Mail gems read_from_string function to get the body but what happens is the mail containing html part is displayed correctly but the one with only text/plain part are displayed continuously without any return character even if the original mail has them... please suggest something... thanks in advance !

MY CODE:

Fetching the mail (I already have the uid)

    email = @imap.uid_fetch(uid, ["BODY[HEADER.FIELDS (FROM TO DATE SUBJECT)]", "RFC822"])

Extracting Headers and the body

header_attr = email[0].attr["BODY[HEADER.FIELDS (FROM TO DATE SUBJECT)]"]
header      = Mail.read_from_string(header_attr)
message_attr = email[0].attr["RFC822"]
message      = Mail.read_from_string(message_attr)

Checking for multipart

if message.multipart?
  @body = message.text_part.decoded.force_encoding("UTF8").encode("UTF-8")
else
  @body = message.body.decoded.force_encoding(message.charset).encode("UTF-8")
end

I use the @body.to_s in my view and it doesn't give correct output :(

content type of a mail

Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit

1
Just a quick guess - you say you are showing the @body in a view - are you certain that the line breaks are indeed not present in the decoded @body? HTML ignores all this whitespace, perhaps you just need to use simple_format(@body) in your view to retain line breaks?Matouš Borák
Great! this thing worked for me straight away :) thanks a lot @BoraMaTanmay Das

1 Answers

0
votes

As pointed out by @BoraMa in the above comment I just had to use simple_format(@body) in my view to display the body