Created
September 25, 2010 01:27
-
-
Save migrs/596342 to your computer and use it in GitHub Desktop.
i.softbank.jp のメールをすべて Gmail に移動する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SB_ACCOUNT = ['USERNAME', 'PASSWORD'] | |
GM_ACCOUNT = ['USERNAME', 'PASSWORD'] | |
BOXCAR_ADDR = "123456.123456@push.boxcar.io" | |
sb.select 'INBOX' | |
imap_each(sb, %w(UNDELETED)) do |mail, m| | |
Net::SMTP.start('127.0.0.1', 25) {|smtp| smtp.send_mail mail.attr['BODY[HEADER]'], m[:from], BOXCAR_ADDR} | |
gm.append 'INBOX', m[:src], nil, m[:date] | |
sb.store mail.seqno, '+FLAGS', [:Deleted] | |
end | |
sb.select 'Sent Messages' | |
imap_each(sb, %w(UNDELETED)) do |mail, m| | |
gm.append '[Gmail]/Sent Mail', m[:src], [:Seen], m[:date] | |
sb.store mail.seqno, '+FLAGS', [:Deleted] | |
end | |
BEGIN { | |
require 'net/imap' | |
require 'net/smtp' | |
require 'time' | |
def sb; @sb ||= imap_login 'imap.softbank.jp', *SB_ACCOUNT; end | |
def gm; @gm ||= imap_login 'imap.gmail.com', *GM_ACCOUNT; end | |
def imap_login host, id, pw | |
imap = Net::IMAP.new host, 993, true | |
imap.login id, pw | |
imap | |
end | |
def imap_each imap, search | |
bodys = %w(BODY[HEADER] BODY[TEXT]) | |
if ids = imap.search(search) and ids.size > 0 | |
imap.fetch(ids, bodys + %w(ENVELOPE)).each do |mail| | |
ev = mail.attr['ENVELOPE'] | |
yield mail, { :date => Time.parse(ev.date), | |
:src => bodys.map{|b|mail.attr[b]}.join, | |
:from => "#{ev.from[0].mailbox}@#{ev.from[0].host}" | |
} | |
end | |
end | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment