Irwan
Irwan

Reputation: 583

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.

Upvotes: 4

Views: 1091

Answers (2)

Irwan
Irwan

Reputation: 583

I found out that we need to fetch different part with this :

<% body = imap.fetch(message_id,'BODY[TEXT]')[0].attr['BODY[TEXT]'] %>
<p><%= body %></p>

Upvotes: 0

Shani
Shani

Reputation: 2541

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

Upvotes: 4

Related Questions