Receive emails from gmail or any other email provider ( IMAP Ruby On Rails )

Configure your gmail for sending emails to other mail clients 

You can retrieve your Gmail messages with a client or device that supports IMAP, like Microsoft Outlook or Apple Mail.

Enable IMAP in your Gmail settings

1. Sign in to Gmail.
2. Click the gear in the top right .
3. Select Settings.
4. Click Forwarding and POP/IMAP.
5. Select Enable IMAP.
6. Click Save Changes.

If you're using a client that's not listed above, you can also use the following information to configure your IMAP. If you have problems, contact your mail client's customer support department for further instructions.

Incoming Mail (IMAP) Server - Requires SSL
Port: 993
Requires SSL:Yes

For more information about imap you can refer that link

After that go to this url and you will find lots of information for allowing access by device or any mail client:
=> You can find out the screenshot of same thing below that I have followed to get access for my ruby on rails application

1. Allow access to your Google account:

2. Access for less secure apps:

After doing all these configurations and setting, we can access the gmail emails using IMAP : 

We are using ruby libraries to fetching emails from gmail  using :
An IMAP client connects to a server, and then authenticates itself using either authenticate() or login().

List sender and subject of all recent messages in the default mailbox

require "net/imap.rb"
imap ='',993,true)
# We are using gmail as a mail provider so below mentioned method can be differ as per mail provider
imap.login('', 'password')
imap.examine('INBOX')["All"]) // Shows all ids of inbox message["All"]) /["RECENT"]).each do |message_id|
  envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
  body = imap.fetch(message_id,'BODY[TEXT]')[0].attr['BODY[TEXT]']
  puts "#{envelope.from[0].name}: \t#{envelope.subject}"

For more information about using imap with ruby you can refer below mentioned link

Example: Imap service that fetch the new updated emails from gmail. We are saving id of last gmail mail in database
class ImapService
  require "net/imap.rb"

  def initialize(params={})
    @email = params[:email] || ''
    @password = params[:password] ||  'testpassword'
    @imap_server = params[:imap_server] ||  ''
    @port = params[:port] || 993
    @ssl = params[:ssl] || true

  def save_emails_in_database
    imap = select_inbox
    latest_email = LatestEmail.last
    ## Check that in database we have stored an id of last fetched email or not
    last_email_id = latest_email.email_id rescue nil
    if last_email_id
      ## We will fetch all emails greater than the saved id in database
      new_email_ids = fetch_latest_emails(imap, last_email_id)
      fetch_body_and_save(imap, new_email_ids)
      last_email_id = new_email_ids.last
      last_email_id = fetch_last_email(imap)
    ## Save last_email_id in database 
    if latest_email.blank?
     latest_email = last_email_id)

  def save_latest_emails_in_database

    attr_reader :email, :password, :imap_server, :password, :port, :ssl

  # attr_reader :address

  def external_imap_service

  def fetch_latest_emails(imap, last_email_id)"#{last_email_id}:*")

  def fetch_all_emails(imap)"all").last

  def connect_to_imap,port,ssl)

  def select_inbox
    imap = connect_to_imap
    imap.login(email, password)
    return imap

  def fetch_body_and_save(imap, new_email_ids)
    new_email_ids.each do |email_id|
      body_text = fetch_email_body(imap, email_id)
      ## Save your emails from here :-)

  def fetch_email_body(imap, email_id)


Post a Comment


  1. Site Traffic: As talked about above, it is hard to see how individuals are drawing in with your image through disconnected promoting.ExcelR Digital Marketing Course in Sydney

  2. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Haga clic aqui

  3. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. Buy Old Gmail Accounts

  4. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. windows iso

  5. thank you for this awesome and informative post

  6. This is a fantastic website and I can not recommend you guys enough. Buy Gmail Accounts

  7. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. Buy Old Gmail Accounts

  8. Attend The Data Analytics Course From ExcelR. Practical Data Analytics Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Course.
    ExcelR Data Analytics Course

  9. you may still get services popping-up and, any attempts to re-install it or install other security software may be denied. If that is what happens in your case, windows 10

  10. He needs to have a comprehension of the business, items and its utilization. He is relied upon to make a format and structure the site. For future extensions, Digital Market service

  11. lines including promoting, visual depiction, website composition and improvement, Advanced organization administrations cross such a large number of the customary assistance

  12. lines including promoting, visual depiction, website composition and improvement, blog comment service

  13. Portable promoting, radio, TV, electronic boards and significantly more are accessible as showcasing outlets. blog comments service in 1$

  14. that your data is unmistakable to the potential client at the fitting time. In the computerized advertising world, Stationary Design

  15. Experience - What sort of aptitudes and experience does the organization have? To what extent has it been working together? Business Card Design

  16. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  17. We provide a range of packages of Google accounts for sale at a reasonable price. Our bundle begins from $30, which is the least fee for getting the full benefit of Google account. All the Google accounts are manually crafted accounts that are entirely worthy of investing your money. We offer the most reasonably priced applications in the market. Buy aged Gmail Accounts