Skip to content

Instantly share code, notes, and snippets.

@mostlyfine
Created January 29, 2013 11:48
Show Gist options
  • Select an option

  • Save mostlyfine/4663684 to your computer and use it in GitHub Desktop.

Select an option

Save mostlyfine/4663684 to your computer and use it in GitHub Desktop.
IMAP振り分け
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'openssl'
require 'net/imap'
require 'yaml'
class Filter
def initialize(options={})
@imap = Net::IMAP.new(options[:host], options[:port] || 993, options[:ssl] || true)
@user = options[:user]
@password = options[:password]
end
def exec(&block)
@imap.login(@user, @password)
@imap.select('INBOX')
block.call(self)
@imap.expunge
@imap.disconnect
end
def move(rule, mailbox)
@imap.search(rule).each do |message_id|
@imap.copy(message_id, mailbox)
@imap.store(message_id, '+FLAGS', [:Deleted])
end
end
end
config = YAML.load_file('mail.yml')
work = Filter.new(config)
work.exec do |filter|
filter.move(['TO', '@gmail.com'], 'INBOX.GMail')
filter.move(['CC', '@yahoo.co.jp'], 'INBOX.Yahoo')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment