I\'m trying to read an email using ruby mail gem.
But mail.body.decoded
returns me not just the body message. How can I clean up this body message and remove unwan
looks like you've got a multipart email, so you can use
mail.parts[0].body.decoded
These will probably come in handy too:
mail.multipart?
mail.parts.length
The gem documentation at github is pretty decent
If you have a properly formatted email, you can use Mail helper methods:
mail = Mail.new(email_string)
mail.text_part # finds the first text/plain part
mail.html_part # finds the first text/html part
This doesn't always work if you have e.g. single part messages (text only) or receive email from the internet at large since you can't rely on formatting from every client out there. Believe me, I've learned the hard way.
Add the mail gem and just use email body format with mail.parts[1].body.decoded.
With the mail
gem, you can do:
text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`