Method: Mail::IMAP#find
- Defined in:
- lib/mail/network/retriever_methods/imap.rb
#find(options = {}, &block) ⇒ Object
Find emails in a IMAP mailbox. Without any options, the 10 last received emails are returned.
Possible options:
mailbox: mailbox to search the email(s) in. The default is 'INBOX'.
what: last or first emails. The default is :first.
order: order of emails returned. Possible values are :asc or :desc. Default value is :asc.
count: number of emails to retrieve. The default value is 10. A value of 1 returns an
instance of Message, not an array of Message instances.
read_only: will ensure that no writes are made to the inbox during the session. Specifically, if this is
set to true, the code will use the EXAMINE command to retrieve the mail. If set to false, which
is the default, a SELECT command will be used to retrieve the mail
This is helpful when you don't want your messages to be set to read automatically. Default is false.
delete_after_find: flag for whether to delete each retreived email after find. Default
is false. Use #find_and_delete if you would like this to default to true.
keys: are passed as criteria to the SEARCH command. They can either be a string holding the entire search string,
or a single-dimension array of search keywords and arguments. Refer to [IMAP] section 6.4.4 for a full list
The default is 'ALL'
search_charset: charset to pass to IMAP server search. Omitted by default. Example: 'UTF-8' or 'ASCII'.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mail/network/retriever_methods/imap.rb', line 73 def find(={}, &block) = () start do |imap| [:read_only] ? imap.examine([:mailbox]) : imap.select([:mailbox]) uids = imap.uid_search([:keys], [:search_charset]) 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? fetchdata = imap.uid_fetch(uid, ['RFC822', 'FLAGS'])[0] = Mail.new(fetchdata.attr['RFC822']) .mark_for_delete = true if [:delete_after_find] if block.arity == 4 yield , imap, uid, fetchdata.attr['FLAGS'] elsif block.arity == 3 yield , imap, uid else yield end imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if [:delete_after_find] && .is_marked_for_delete? break unless [:uid].nil? end imap.expunge if [:delete_after_find] else emails = [] uids.each do |uid| uid = [:uid].to_i unless [:uid].nil? fetchdata = imap.uid_fetch(uid, ['RFC822'])[0] emails << Mail.new(fetchdata.attr['RFC822']) imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if [:delete_after_find] break unless [:uid].nil? end imap.expunge if [:delete_after_find] emails.size == 1 && [:count] == 1 ? emails.first : emails end end end |