Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Gets Body Message IMAP

I am currently developing Mail application where the application can send emails and have inbox.

So, I follow this guide.

imap = Net::IMAP.new('mail.example.com')
imap.authenticate('LOGIN', 'joe_user', 'joes_password')
imap.examine('INBOX')
imap.search(["RECENT"]).each do |message_id|
  envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
  puts "#{envelope.from[0].name}: \t#{envelope.subject}"
end

But, I can't get the content of the email. I can't find any variables that keeps the content of the email. Could someone help me? Thank you.

like image 985
Irwan Avatar asked Oct 27 '25 06:10

Irwan


1 Answers

Following is working for me with gmail as in explained here imap mail

require 'net/imap'

imap = Net::IMAP.new("imap.gmail.com", 993, usessl = true)

  imap.login("username", "password")
  imap.examine('INBOX')
  imap.search(["ALL"]).each do |message_id|
    env = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
    raw_message = imap.fetch(message_id,'RFC822').first.attr['RFC822']
    puts "#{env.from[0].name}: \t#{env.subject}"
    puts "raw message: #{raw_message}"
  end
like image 198
Shani Avatar answered Oct 28 '25 21:10

Shani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!