Class: WatirmarkEmail::BaseController

Inherits:
Object
  • Object
show all
Defined in:
lib/watirmark_email/email_base.rb

Direct Known Subclasses

Gmail, QAMail

Instance Method Summary collapse

Instance Method Details

#connectObject

Connects using the credentials supplied on initialize, returns the IMAP object. Most of the time, if all you want is an email, you won’t need to use this method as get_email_text handles connect for you.



5
6
7
8
9
10
# File 'lib/watirmark_email/email_base.rb', line 5

def connect
  imap = Net::IMAP.new(@url, @port, @ssl)
  imap.(@email, @password)
  imap.select(@inbox)
  imap
end

#copy(search_array, destination_folder = @inbox) ⇒ Object

This is a test method to copy an email to the inbox



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/watirmark_email/email_base.rb', line 20

def copy(search_array, destination_folder=@inbox)
  begin
    @log.debug("copying to #{destination_folder}")
    imap      = connect
    email_id  = imap.search(search_array).last
    email_uid = imap.fetch(email_id, 'UID').last.attr['UID']
    raise "Original message not found to copy!" unless email_uid
    imap.uid_copy(email_uid, destination_folder)
  ensure
    disconnect(imap)
  end
end

#disconnect(imap) ⇒ Object

This is a test method to disconnect from the server. get_email_text handles disconnect for you.



13
14
15
16
17
# File 'lib/watirmark_email/email_base.rb', line 13

def disconnect(imap)
  return true if imap.disconnected?
  imap.logout
  imap.disconnect
end

#get_email_attachment(search_array, timeout = 600) ⇒ Object

returns the name of the email attachment returns nil if there is no attachment



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/watirmark_email/email_base.rb', line 104

def get_email_attachment(search_array, timeout=600)
  attachment = nil
  finished = false
  ::Timeout.timeout(timeout) do
    @log.debug("start Timeout block for #{timeout} seconds")
    loop do
      begin
        imap = connect
        msgs = imap.search(search_array)
        if (msgs && msgs.length > 0)
          msgs.each do |msgID|
            msg = imap.fetch(msgID, ["ENVELOPE", "UID", "BODY"])[0]
            body = msg.attr["BODY"]
            attachment = body.parts[1].param['NAME']
            finished = true
            #TODO read text of .pdf file
            #grab attachment file
            #attachment_file = imap.fetch(msgID, "BODY[#{2}]")[0].attr["BODY[#{2}]"]
          end
        end
      rescue => e
        @log.info("Error connecting to IMAP: #{e.message}")
      ensure
        disconnect(imap) unless imap.nil? # because sometimes the timeout happens before imap is defined
      end
      break if finished
      @log.debug("Couldn't find email yet ... trying again")
      sleep 10
    end
  end
  attachment
end

#get_email_replyto(search_array, timeout = 600, delete = true) ⇒ Object



71
72
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
# File 'lib/watirmark_email/email_base.rb', line 71

def get_email_replyto(search_array, timeout=600, delete=true)
  envelope = nil
  email_uid = nil

  ::Timeout.timeout(timeout) do
    @log.debug("start Timeout block for #{timeout} seconds")
    loop do
      begin
        imap = connect
        msgs = imap.search(search_array)
        if (msgs && msgs.length > 0)
          email_id = msgs.last
          email_uid = imap.fetch(email_id, 'UID').last.attr['UID']
          envelope = imap.uid_fetch(email_uid, 'ENVELOPE').last.attr['ENVELOPE']
        end
      rescue => e
        @log.info("Error connecting to IMAP: #{e.message}")
      ensure
        if (delete && email_uid)
          delete(email_uid, imap)
        end
        disconnect(imap) unless imap.nil? # because sometimes the timeout happens before imap is defined
      end
      break if envelope
      @log.debug("Couldn't find email yet ... trying again")
      sleep 10
    end
  end
  "#{envelope.reply_to[0].name} <#{envelope.reply_to[0].mailbox}@#{envelope.reply_to[0].host}>"
end

#get_email_text(search_array, timeout = 600, delete = true, since_sec = 3600) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
# File 'lib/watirmark_email/email_base.rb', line 33

def get_email_text(search_array, timeout=600, delete=true, since_sec=3600)
  # Trying super ugly workaraound for the gmail 'Too many simlutaneous connections' error.
  # Just trying to login to gmail and if it fails try to wait until other connections
  # are closed and try again.
  email_text    = nil
  email_subject = nil
  email_uid     = nil

  ::Timeout.timeout(timeout) do
    @log.debug("start Timeout block for #{timeout} seconds")
    loop do
      begin
        imap = connect
        msgs = imap.search(search_array)
        if (msgs && msgs.length > 0)
          email_id      = msgs.last
          email_uid     = imap.fetch(email_id, 'UID').last.attr['UID']
          email_text    = imap.uid_fetch(email_uid, 'BODY[TEXT]').last.attr['BODY[TEXT]']
          envelope      = imap.uid_fetch(email_uid, 'ENVELOPE').last.attr['ENVELOPE']
          email_subject = envelope.subject
        end
      rescue => e
        @log.info("Error connecting to IMAP: #{e.message}")
      ensure
        if (delete && email_uid)
          @log.info("Deleting the email message #{email_subject}")
          delete(email_uid, imap)
        end
        disconnect(imap) unless imap.nil? # because sometimes the timeout happens before imap is defined
      end
      break if email_text
      @log.debug("Couldn't find email yet ... trying again")
      sleep 10
    end
  end
  email_text
end