Created
October 14, 2010 09:22
-
-
Save masahitojp/625926 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"pop_host" : "host.com", | |
"pop_port" : 110, | |
"pop_userid" : "userid", | |
"pop_passwd" : "passwd" | |
} |
This file contains hidden or 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
# -*- encoding: utf-8 -*- | |
require 'rubygems' | |
require 'tmail' | |
require 'net/pop' | |
require 'json' | |
jsondata =open("./mail.json").read | |
setting = JSON.parse(jsondata) | |
pop_host = setting["pop_host"] | |
pop_port = setting["pop_port"] | |
pop_userid = setting["pop_userid"] | |
pop_passwd = setting["pop_passwd"] | |
MailDir = './inbox/' | |
Dir::mkdir(MailDir) if not FileTest::directory?(MailDir) | |
begin | |
pop3 = Net::POP3.new(pop_host, pop_port) | |
pop3.start(pop_userid, pop_passwd) do |pop| | |
unless pop.mails.empty? | |
mailnum=0 | |
pop.mails.each do |m| | |
begin | |
mailnum += 1 | |
mail = TMail::Mail.parse(m.pop) | |
# tmail 1.2.7.1 の不具合? subjectが日本語の場合、取得しようとすると以下エラーとなる | |
# incompatible encoding regexp match (US-ASCII regexp with ISO-2022-JP string) | |
begin | |
sbj = mail.subject | |
rescue | |
sbj="default" | |
end | |
# win環境用にtosjisしている。 | |
puts "====================================================" | |
puts "[#{mailnum}]" | |
puts "#{mail.from}:[#{sbj.tosjis}] \n #{mail.body.tosjis}" | |
# 添付ファイル保存 | |
mail.parts.each do |part| | |
part.base64_decode | |
if part['content-disposition'] | |
tmpfile = part['content-disposition']['filename'].tosjis | |
savedir = "#{MailDir}/#{mailnum}/" | |
Dir::mkdir(savedir) if not FileTest::directory?(savedir) | |
File.open("#{savedir}#{tmpfile}", 'w') {|f| | |
f.write part.body | |
} | |
end | |
end | |
puts "====================================================" | |
# 出力後、メールを削除 | |
#m.delete | |
rescue Exception => e | |
puts "Error " + Time.now.to_s + "::: " + e.message | |
end | |
end | |
end | |
end | |
rescue Net::POPAuthenticationError => e | |
puts "Error pop authentication at " + Time.now.to_s + "::: " + e.message | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment