Receiving incoming emails in rails 3


Well..we all probably use our web applications for sending email for any kind of notifications but when it comes to receiving the email from gmail or any other email service, we rarely do. I also ran into same rarest scenario where i had receive email from gmail. So, I will be stick to using gmail as my email service provider but you can use any of your interest.

Receiving email in Rails however is slightly less documented. But there are many options available for it-from ruby gems to rails native way of receiving it and choosing the right path is completely subject of one’s requirement and choice. However please have a look at below link for some available options and pros and cons associated with it.

Receiving Incoming Email in Rails 3 – choosing the right approach

Looking at the above link, I decided to go for using IMAP. We do not need any external gem for it as ruby provide us the library to accomplish the task.

Before writing any code for it, we need to have a imap configured gmail account. If you do not imap configured, please configure it before moving ahead. Below is the process to enable imap in gmail:
1. Sign in to Gmail.
2. Click the gear icon in the upper right, then select Settings.
3. Click Forwarding and POP/IMAP.
4. Select Enable IMAP.
5. Click Save Changes.

Now create a yml file say config/imap.yml to store imap credentials.

  host: imap.gmail.com
  port: 993
  username: ****************
  password: ****************
  enable_ssl: true

Now we are all set to fetch email from gmail using IMAP. Now use the below code to fetch and decode email.


  require 'net/imap'
  require 'net/http'

  # imap.yml contains the imap config for the email account (ie: username, host, etc.)
  config = YAML.load(File.read(File.join(Rails.root, 'config', 'imap.yml')))

  #create imap instance and authenticate application
  imap = Net::IMAP.new(config['host'],config['port'],true)
  imap.login(config['username'], config['password'])

  #select inbox of gmail for message fetching
  imap.select('INBOX')

  #fetch all messages that has not been marked deleted
  imap.search(["NOT", "DELETED"]).each do |mail|

    message = Mail.new(imap.fetch(mail, "RFC822")[0].attr["RFC822"])

    #fetch plain part message from multipart gmail content
    plain_body = message.multipart? ? (message.text_part ? message.text_part.body.decoded : nil) : message.body.decoded

    #fetch to and from email address.. you can fetch other mail headers too in same manner.
    to_email = message.to
    from_email = message.from

    # do whatever you want to do with those data...like print it
    puts plain_body
    puts to_email
    puts from_email

    #mark message as deleted to remove duplicates in fetching
    imap.store(mail, "+FLAGS", [:Deleted])

  end
  #logout and close imap connection
  imap.logout()
  imap.disconnect()

Now you have successfully fetched email from gmail…yay…cheers 🙂

2 thoughts on “Receiving incoming emails in rails 3

Leave a comment