Skip to content

Instantly share code, notes, and snippets.

@vivahiraj
Last active December 29, 2015 08:08
Show Gist options
  • Save vivahiraj/7640966 to your computer and use it in GitHub Desktop.
Save vivahiraj/7640966 to your computer and use it in GitHub Desktop.
imapでfromが特定アドレスにマッチしたメールだけを削除する
# -*- encoding: utf-8 -*-
require 'net/imap'
require 'kconv'
SVR='sample.server.com'
USR='user'
PWD='pppppp'
ADR='[email protected]'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment