Method: Mail::ImapTools#status_or_create_mailbox

Defined in:
lib/mail/imap_tools.rb

#status_or_create_mailbox(mailbox, raise_on_select = false) ⇒ Object

Checks whether the given mailbox exists and creates it if not. If it exists it will be selected. TODO check imap protocoll if there isn’t a better way to do this.

Parameters

mailbox

Name of the mailbox (imap folder)

raise_on_select

Raise an exception if a select is not possible. This also skips creation of the mailbox. Used for internal purposes.



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
# File 'lib/mail/imap_tools.rb', line 25

def status_or_create_mailbox(mailbox, raise_on_select = false)
  ret = nil
  
  begin
    ret = @imap.status(mailbox, ["MESSAGES", "RECENT", "UNSEEN"])
    # Mailbox already exists
  rescue Net::IMAP::NoResponseError => e

    # The mailbox does not exist (or is non-selectable for some reason)
    unless raise_on_select
      
      # So we create it
      @imap.create(mailbox)
      puts "Created mailbox #{mailbox}."

      # And select it
      status_or_create_mailbox(mailbox, true)
    else
      
      # For some reasons the creation/selection of the mailbox failed.
      # In order to avoid an infinte loop we give up after trying to create and select the mailbox once.
      raise e
    end
  end
  return ret
end