2013-11-25

imapで特定のfromアドレスのメールだけを削除するものをrubyで書いてみた

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
imapを利用しているメールで見取り込みのメールの中から特定のfromアドレスからのメールだけを削除するというものをrubyで書いてみました。

以下のような感じです。


# -*- encoding: utf-8 -*-
require 'net/imap'
require 'kconv'
SVR='sample.server.com'
USR='user'
PWD='pppppp'
ADR='target@sample.com'
class Net::IMAP::Envelope
def mail_address_formatted(value)
return nil unless ["from", "sender", "reply_to", "to"].include?(value)
self.__send__(value)[0].mailbox + "@" + self.__send__(value)[0].host
end
end
imap = Net::IMAP.new(SVR)
imap.login(USR,PWD)
imap.select('INBOX')
imap.search(['UNSEEN']).each do |msg_id|
envelope = imap.fetch(msg_id, "ENVELOPE")[0].attr["ENVELOPE"]
from = envelope.mail_address_formatted("from")
puts "#{msg_id}:#{from}:#{envelope.subject.toutf8}"
if from == ADR
puts "delete"
imap.store(msg_id, "+FLAGS", [:Deleted])
end
end
imap.expunge

0 件のコメント: