ruby mailman remove header from email body

994 views Asked by At

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:
> <[email protected]> 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.

1

There are 1 answers

1
user1402147 On

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