Class: Mail::IMAP

Inherits:
Object
  • Object
show all
Defined in:
lib/mailhandler/receiving/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#imap_connectionObject

Returns the value of attribute imap_connection.



5
6
7
# File 'lib/mailhandler/receiving/mail.rb', line 5

def imap_connection
  @imap_connection
end

Instance Method Details

#connect(config = Mail::Configuration.instance) ⇒ Object

Start an IMAP session



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mailhandler/receiving/mail.rb', line 62

def connect(config=Mail::Configuration.instance)

  @imap_connection = Net::IMAP.new(settings[:address], settings[:port], settings[:enable_ssl], nil, false)

  if settings[:authentication].nil?
    imap_connection.(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

#disconnectObject



76
77
78
79
80
81
82
83
84
# File 'lib/mailhandler/receiving/mail.rb', line 76

def disconnect

  if defined?(imap_connection) && imap_connection && !imap_connection.disconnected?

    imap_connection.disconnect

  end

end

#find_emails(options = {}, &block) ⇒ Object



7
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
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mailhandler/receiving/mail.rb', line 7

def find_emails(options={}, &block)

  options = validate_options(options)
  options[:read_only] ? imap_connection.examine(options[:mailbox]) : imap_connection.select(options[:mailbox])
  uids = imap_connection.uid_search(options[:keys])

  uids.reverse! if options[:what].to_sym == :last
  uids = uids.first(options[:count]) if options[:count].is_a?(Integer)
  uids.reverse! if (options[:what].to_sym == :last && options[:order].to_sym == :asc) ||
      (options[:what].to_sym != :last && options[:order].to_sym == :desc)

  if block_given?

    uids.each do |uid|

      uid = options[:uid].to_i unless options[:uid].nil?
      fetchdata = imap_connection.uid_fetch(uid, ['RFC822'])[0]
      new_message = Mail.new(fetchdata.attr['RFC822'])
      new_message.mark_for_delete = true if options[:delete_after_find]

      if block.arity == 3
        yield new_message, imap_connection, uid
      else
        yield new_message
      end

      imap_connection.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find] && new_message.is_marked_for_delete?
      break unless options[:uid].nil?

    end

    imap_connection.expunge if options[:delete_after_find]

  else

    emails = []

    uids.each do |uid|

      uid = options[:uid].to_i unless options[:uid].nil?
      fetchdata = imap_connection.uid_fetch(uid, ['RFC822'])[0]
      emails << Mail.new(fetchdata.attr['RFC822'])
      imap_connection.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find]
      break unless options[:uid].nil?

    end

    imap_connection.expunge if options[:delete_after_find]
    emails.size == 1 && options[:count] == 1 ? emails.first : emails

  end

end