ConradIrwin / em-imap

An event machine based IMAP client
MIT License
69 stars 31 forks source link

Idler receive email only for first time #7

Closed nazarhussain closed 11 years ago

nazarhussain commented 11 years ago

Hi,

I want to write IMAP script which reads the emails from gmail IMAP as they arrived. I wrote the script give below. It works perfectly fine. But the issue is It will only read email for first time. I run the script and then send an email to myemail@gmail.com and it appears on the console but it will never work for next email.

I tried to remove the line idler.stop but this will also block the fetch callback. Please look at the code and guide me the issue in it.

EM.run do
  client = EM::IMAP.new(config['host'], config['port'], true)
  client.connect.bind! do
    client.login(myemail@gmail.com', 'my_password')
  end.bind! do
    client.select("INBOX")
  end.bind! do
    idler = client.idle
    puts idler.inspect
    idler.listen do |response|
      if (response.name == "EXISTS" rescue nil)
        puts "Ooh, new emails!"
        idler.stop
        client.fetch(response.data).callback do |email|
          puts email.inspect
          Model.delay.process_incoming_mail(email)
        end
      end
    end.errback do |e|
      puts "Idler recieved an error: #{e}"
    end
  end
  Signal.trap("INT") { client.disconnect; EM.stop }
  Signal.trap("TERM") { client.disconnect; EM.stop }
end
ConradIrwin commented 11 years ago

I've made a new API for IDLE: https://github.com/ConradIrwin/em-imap#idle Hopefully it's easier to use! You can also use wait_for_one_email if you only want one.

nazarhussain commented 11 years ago

Thanks. It really will help a lot.