rubyemailruby-1.9

How to delete specific mail on server by ruby/mikel mail?


I want to use Ruby/Mikel Mail gem to access pop3, but am stuck finding a way to SELECTIVELY delete mails from the server. Here's some disfunctional example code which examplifies how I misunderstand the rdoc specs provided.

require 'rubygems'
require 'mail'

Mail.defaults do
  retriever_method :pop3, { :address => '...',                        
                     :user_name  => '...',
                     :password   => '...' }                        
end


puts "#{Mail.all.length} messages on server found."
if Mail.all.length > 0 
  mm = Mail.first
  puts mm.from
  puts "I delete all but the first mail!"

  mm.mark_for_delete = false
  Mail.find_and_delete

  puts "#{Mail.all.length} messages on server found."  
end

The result is that with 2 mails on the server, this script just deletes both. Instead I only want it to delete the first.


Solution

  • Found it myself, answer for you if you are looking at same problem..

    Turns out you have to throw a Block at find_and_delete(), to explicitly decide to skip deletion of messages, simply:

    Mail.find_and_delete({:what=>:first}) { |msg| 
         msg.skip_deletion if msg.subject == "test1826"
    }