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
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 thefetch callback
. Please look at the code and guide me the issue in it.