Skip to content

Instantly share code, notes, and snippets.

@mirakui
Created April 29, 2009 03:01
Show Gist options
  • Save mirakui/103553 to your computer and use it in GitHub Desktop.
Save mirakui/103553 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/imap'
require 'kconv'
require 'pit'
require 'logger'
class ImapFetcher
def initialize(pit_name)
@pit = Pit.get(
pit_name,
:require=>{
'host'=>'imap.gmail.com',
'port'=>993,
'use_ssl'=>'true',
'login'=>'',
'password'=>''
}
)
end
def logger
unless @logger
@logger = Logger.new(STDOUT)
@logger.level = Logger::DEBUG
end
@logger
end
def logger=(logger)
@logger = logger
end
def fetch(filter)
mails = []
begin
# GmailにIMAPで接続、ログインする
@imap = Net::IMAP.new( @pit['host'], @pit['port'].to_i, @pit['use_ssl']=='true' )
logger.debug 'IMAP.new'
@imap.login( @pit['login'], @pit['password'] ) # ID、パスワード
logger.debug "IMAP LOGIN #{@pit['login'].inspect}"
# 受信箱を開く
@imap.select( 'INBOX' )
logger.debug 'IMAP SELECT INBOX'
# メールを開く
logger.debug "IMAP SEARCH #{filter.inspect}"
@imap.search( filter ).each do | msg_id |
logger.debug "FOUND msg_id:#{msg_id.inspect}"
# 本文を取得する
fetch_attr = '(UID RFC822.SIZE ENVELOPE BODY[HEADER] BODY[TEXT])'
msg = @imap.fetch( msg_id, fetch_attr ).first
logger.debug "IMAP FETCH #{msg_id.inspect} #{fetch_attr.inspect}"
fetched_mail = {
:body => msg.attr['BODY[TEXT]'].toutf8,
:header => msg.attr['BODY[HEADER]'].toutf8
}
mails.push fetched_mail
logger.debug "PUSHED #{fetched_mail[:header][/^Subject:.*$/]}"
# 既読のフラグを立てる
@imap.store( msg_id, '+FLAGS', [:Seen] )
logger.debug "IMAP STORE #{msg_id.inspect} +FLAGS SEEN"
#@imap.store( msg_id, '+FLAGS', [:Deleted] )
end
# フラグを反映
@imap.expunge
logger.debug "IMAP EXPUNGE"
rescue Object=>e
raise e # 例外処理
ensure
# 切断する
@imap.logout
logger.debug "IMAP LOGOUT"
#@imap.disconnect
#logger.debug "IMAP DISCONNECT"
end
mails
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment