Module: Mail::ImapTools

Included in:
EmailToSms
Defined in:
lib/mail/imap_tools.rb

Instance Method Summary collapse

Instance Method Details

#delete_email(uid) ⇒ Object

Deletes the email with the given uid



6
7
8
9
# File 'lib/mail/imap_tools.rb', line 6

def delete_email(uid)
  @imap.uid_store(uid, "+FLAGS", [:Deleted])
  @imap.expunge
end

#move_email(uid, mailbox) ⇒ Object

Move a mail from the current to the given mailbox.



12
13
14
15
16
17
# File 'lib/mail/imap_tools.rb', line 12

def move_email(uid, mailbox)

  # Copy mail to sent_sms folder
  @imap.uid_copy(uid, mailbox)
  delete_email(uid)
end

#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