2

I am trying to display an email body in my RoR project.

class IncomingMail
  def initialize(message, params)
    if person = Person.find_by_email(message.from)
      changeMessage = Message.where({person_id: person.id})

      #message = message.subject.force_encoding("UTF-8")

      message = message.body.encoded

      changeMessage.first.text = message
      changeMessage.first.backInMinutes = 0
      changeMessage.first.showText = 1
      changeMessage.first.doNotDisturb = 0
      changeMessage.first.save
    end
  end

but i also get the email header

> --e89a8ff1c0465030f204c082e054 Date: Mon, 21 May 2012 04:45:12 +0200 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: 7bit Content-ID:
> <4fb9ac38c71d2_1e1343dd8042105b@ubuntu.mail> Text of the mail

how can I remove the header ?

with the subject it works like this

message = message.subject.force_encoding("UTF-8")

But not with the body.

mihai
  • 37,072
  • 9
  • 60
  • 86
user1402147
  • 113
  • 1
  • 7
  • ActiveMailer automatically encodes the subject and body in UTF-8. So, you don't have to do any custom encoding for that. And could you explain where you get the header? An Email will always have a header with from, to, subject fields. – Salil May 21 '12 at 06:55
  • i get that thinks.......--e89a8ff1c0465030f204c082e054 Date: Mon, 21 May 2012 04:45:12 +0200 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 > Content-Transfer-Encoding: 7bit Content-ID: > <4fb9ac38c71d2_1e1343dd8042105b@ubuntu.mail>....... and then continue the text of the mail .... but i only want the text and not the thinks before – user1402147 May 21 '12 at 14:29
  • The problem still exists. I want to display the email somewher else. but not with that header stuff only the clean text. – user1402147 May 21 '12 at 21:27

1 Answers1

7

I have found the solution!!!

change:

message = message.body.encoded

to:

message = message.text_part.body.decoded

that cuts all the header details away and gives me only the TEXT of the email.

It took a long time but it worked I hope it also helps other users

user1402147
  • 113
  • 1
  • 7
  • It seems like `message.text_part` is sufficient. I didn't notice any effect from `.decoded`. I also didn't notice any difference between `.encoded` and `.decoded`. – Dennis Mar 24 '14 at 21:27