Method: Client#imap_connection

Defined in:
lib/client.rb

#imap_connection(server, username, password) ⇒ Object

imap_connection

connect the app with imap server



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/client.rb', line 36

def imap_connection(server, username, password)
  # connect to IMAP server
  imap = Net::IMAP.new server, ssl: true, certs: nil, verify: false

  Net::IMAP.debug = @net_imap_debug

  # http://ruby-doc.org/stdlib-2.1.5/libdoc/net/imap/rdoc/Net/IMAP.html#method-i-capability
  capabilities = imap.capability

  @logger.debug("imap capabilities: #{capabilities.join(',')}") if @debug

  if @client_type == 'local'
    unless capabilities.include? "IDLE" && @debug
      @logger.info "'IDLE' IMAP capability not available in server: #{server}"
      imap.disconnect
      exit
    end
  end

  begin
    # login
    imap. username, password
  rescue Net::IMAP::NoResponseError => e
    # mostly due to credentials error
    @errors = {exception: e}
  rescue Net::IMAP::BadResponseError => e
    @errors = {exception: e}
  end

  # return IMAP connection handler
  imap
end