[ruby-list:50558] IMAP IDLE

From: emo@...
Date: 2017-08-21 22:37:20 UTC
List: ruby-list #50558
DOCOMO のキャリアメールで届いたメールを
リアルタイムに通知するためのプログラムを
作ろうとしているのですがうまく行きません。

インターネット上のブログ等の情報によると、
DOCOMO のメールで IMAP の IDLE を使って
プッシュ送信を実現しているという様な記事
があったので、下記のようなプログラムを書いた
のですが、Net::IMAP#idle を実行した時点で
接続が切れてしまうようなのですが、
どのようにすれば良いのでしょうか?

同じプログラムで gmail では正しく動いている
ことは確認しました。


江本


== 実行結果 ==

:~/src/ruby/imap ./test_imap2.rb
connected to imap server
["IMAP4REV1", "NAMESPACE", "IDLE", "LITERAL+", "UIDPLUS", "QUOTA", "AUTH=LOGIN", 
"ID"]
#<Net::IMAP::Error: connection closed>
connection closed: reconecting...
connected to imap server
["IMAP4REV1", "NAMESPACE", "IDLE", "LITERAL+", "UIDPLUS", "QUOTA", "AUTH=LOGIN", 
"ID"]
#<Net::IMAP::Error: connection closed>
connection closed: reconecting...
connected to imap server
["IMAP4REV1", "NAMESPACE", "IDLE", "LITERAL+", "UIDPLUS", "QUOTA", "AUTH=LOGIN", 
"ID"]
#<Net::IMAP::Error: connection closed>

== プログラム ==

#!/usr/bin/env ruby
# coding: utf-8
require 'net/imap'
require 'kconv'
require 'time'


imap_user ="ユーザ名"
imap_passwd = "パスワード"
imap_server = 'imap.spmode.ne.jp'

#imap_user ="ユーザ名"
#imap_passwd = "パスワード"
#imap_server = 'imap.gmail.com'


attr_name = 'BODY[HEADER.FIELDS (SUBJECT)]'
attr_addr = 'BODY[HEADER.FIELDS (FROM)]'

last_id = -1
while true
   begin
     unless $imap
       $imap = Net::IMAP.new(imap_server, 993, true)
       $imap.login(imap_user, imap_passwd)
       $imap.select('INBOX')
       puts "connected to imap server"
     end
     capabilities = $imap.capability
     p capabilities
     $imap.idle do |resp|
       if resp.name == "EXISTS"
         print "New Mail Arrived\n"
         last_id = resp.data
         $imap.idle_done
       else
         p resp
       end
     end

   rescue Net::IMAP::Error => e
     p e
     if e.inspect.include? "connection closed"
       puts "connection closed: reconecting..."
       $imap = nil
     else
       raise
     end
   end

   next unless $imap

   msgs = $imap.fetch((last_id..-1), [attr_name, attr_addr])
   msgs.each do |msg|
     subject = msg.attr[attr_name].toutf8.strip
     from = msg.attr[attr_addr].toutf8.strip
     print "#{from} #{subject}\n"
   end

end

== 実行環境 ==

% lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial
%  ruby --version
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

Attachments (1)

smime.p7s (3.91 KB, application/pkcs7-signature)

In This Thread

Prev Next