0

I am using ruby Gmail gem to read coming emails. I can successfully get date/subject/mailbox/host but I couldn't extract email body from email.body. Basically, I want to see only "Last First A Title Date last accessed https://www.google.com" but I am getting bellow.

def main
   gmail = Gmail.connect(USERNAME, PASSWORD)
   gmail.inbox.find(:unread).each do |email|
      puts email.body

   end
   gmail.logout
end

if __FILE__ == $PROGRAM_NAME
   main()
end

It returns

--001a113d2dde4115020541d12a51
Content-Type: text/plain; charset=UTF-8

Last First
A Title
Date last accessed
https://www.google.com

--001a113d2dde4115020541d12a51
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><span style=3D"font-size:16px">Last First</span></div=
><div><span style=3D"font-size:16px">A Title</span></div><div><div style=3D=
"font-size:12.8px"><span style=3D"font-size:16px">Date last accessed</span>=
<span style=3D"font-size:16px"><br></span><div><a href=3D"https://www.googl=
e.com" target=3D"_blank">https://www.google.com</a></div></div></div><div><=
br><br></div></div>

--001a113d2dde4115020541d12a51--
jeton
  • 841
  • 12
  • 15
  • Date, subject and the rest are part of the envelope, not the body. Your example looks like a valid body in [MIME](https://en.wikipedia.org/wiki/MIME) format was extracted. Perhaps you need to decode the MIME? – the Tin Man Nov 21 '16 at 16:20

1 Answers1

1

Gmail gem uses Mail gem so solution is coming from mail gem / mail gem questions. I follow => enter link description here

#!/usr/bin/ruby
require 'gmail'
USERNAME = 'xxxxxxx@gmail.com'
PASSWORD = 'xxxxxxxx'

def main
    gmail = Gmail.connect(USERNAME, PASSWORD)
    gmail.inbox.find(:unread).each do |email|
        puts email.text_part ? email.text_part.body.decoded : nil   
        #puts email.html_part ? email.html_part.body.decoded : nil
    end
    gmail.logout
end

if __FILE__ == $PROGRAM_NAME
    main()
end
Community
  • 1
  • 1
jeton
  • 841
  • 12
  • 15