Class: Mail::IMAP
- Inherits:
-
Object
- Object
- Mail::IMAP
- Defined in:
- lib/mailhandler/extensions/mail/imap.rb
Overview
IMAP class patch to better manage connection and search of emails
Instance Attribute Summary collapse
-
#imap_connection ⇒ Object
Returns the value of attribute imap_connection.
Instance Method Summary collapse
-
#connect(_config = Mail::Configuration.instance) ⇒ Object
Start an IMAP session.
- #disconnect ⇒ Object
-
#find_emails(options = {}, &block) ⇒ Object
rubocop:disable all.
Instance Attribute Details
#imap_connection ⇒ Object
Returns the value of attribute imap_connection.
6 7 8 |
# File 'lib/mailhandler/extensions/mail/imap.rb', line 6 def imap_connection @imap_connection end |
Instance Method Details
#connect(_config = Mail::Configuration.instance) ⇒ Object
Start an IMAP session
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mailhandler/extensions/mail/imap.rb', line 51 def connect(_config = Mail::Configuration.instance) # rubocop:disable all @imap_connection = Net::IMAP.new(settings[:address], **imap_parameters) if settings[:authentication].nil? imap_connection.login(settings[:user_name], settings[:password]) else # Note that Net::IMAP#authenticate('LOGIN', ...) is not equal with Net::IMAP#login(...)! # (see also http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/imap_connection/rdoc/classes/Net/IMAP.html#M000718) imap_connection.authenticate(settings[:authentication], settings[:user_name], settings[:password]) end end |
#disconnect ⇒ Object
63 64 65 |
# File 'lib/mailhandler/extensions/mail/imap.rb', line 63 def disconnect imap_connection.disconnect if defined?(imap_connection) && imap_connection && !imap_connection.disconnected? end |
#find_emails(options = {}, &block) ⇒ Object
rubocop:disable all
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mailhandler/extensions/mail/imap.rb', line 8 def find_emails( = {}, &block) # rubocop:disable all = () [:read_only] ? imap_connection.examine([:mailbox]) : imap_connection.select([:mailbox]) uids = imap_connection.uid_search([:keys]) uids.reverse! if [:what].to_sym == :last uids = uids.first([:count]) if [:count].is_a?(Integer) uids.reverse! if ([:what].to_sym == :last && [:order].to_sym == :asc) || ([:what].to_sym != :last && [:order].to_sym == :desc) if block_given? uids.each do |uid| uid = [:uid].to_i unless [:uid].nil? = retrieve_email_content(uid, [:fetch_type]) .mark_for_delete = true if [:delete_after_find] if block.arity == 3 yield , imap_connection, uid else yield end imap_connection.uid_store(uid, '+FLAGS', [Net::IMAP::DELETED]) if [:delete_after_find] && .is_marked_for_delete? break unless [:uid].nil? end imap_connection.expunge if [:delete_after_find] else emails = [] uids.each do |uid| uid = [:uid].to_i unless [:uid].nil? emails << retrieve_email_content(uid, [:fetch_type]) imap_connection.uid_store(uid, '+FLAGS', [Net::IMAP::DELETED]) if [:delete_after_find] break unless [:uid].nil? end imap_connection.expunge if [:delete_after_find] emails.size == 1 && [:count] == 1 ? emails.first : emails end end |