Class: WatirmarkEmail::QAMail
Constant Summary
collapse
- URL =
"qasendmail.conviocloud.com"
- PORT =
143
- MAILBOX_INBOX =
"Inbox"
Instance Method Summary
collapse
-
#delete(email_uid, imap) ⇒ Object
-
#delete_emails(search_terms, timeout = 60) ⇒ Object
-
#find_email_uids(search_terms, timeout = 60) ⇒ Object
-
#find_emails(search_terms, timeout = 60) ⇒ Object
-
#get_email_attachment(search_arry, timeout = 600) ⇒ Object
-
#get_email_replyto(search_array, timeout = 600, delete = true) ⇒ Object
-
#get_email_text(search_array, timeout = 600, delete = true) ⇒ Object
This keeps polling the email inbox until a message is found with the given parameters (based on net::IMAP search) or the timeout is reached.
-
#initialize(account, password = nil, logLevel = ::Logger::INFO) ⇒ QAMail
constructor
Constructor for this class.
-
#send_email(to, opts = {}) ⇒ Object
#connect, #copy, #disconnect
Constructor Details
#initialize(account, password = nil, logLevel = ::Logger::INFO) ⇒ QAMail
Constructor for this class. This will initialize all variables according to the type email service this is using.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/watirmark_email/qamail.rb', line 9
def initialize(account, password=nil, logLevel = ::Logger::INFO)
@email = account
@password = password || account
@log = ::Logger.new STDOUT
@log.level = logLevel
@url = URL
@port = PORT
@inbox = MAILBOX_INBOX
@ssl = false end
|
Instance Method Details
#delete(email_uid, imap) ⇒ Object
20
21
22
23
|
# File 'lib/watirmark_email/qamail.rb', line 20
def delete(email_uid, imap)
imap.uid_store(email_uid, "+FLAGS", [:Deleted])
imap.expunge
end
|
#delete_emails(search_terms, timeout = 60) ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/watirmark_email/qamail.rb', line 26
def delete_emails(search_terms, timeout = 60)
uid_list = find_email_uids(search_terms, timeout)
imap = connect
uid_list.each do |uid|
imap.uid_store(uid, "+FLAGS", [:Deleted])
end
imap.expunge
disconnect imap
end
|
#find_email_uids(search_terms, timeout = 60) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/watirmark_email/qamail.rb', line 131
def find_email_uids(search_terms, timeout = 60)
email_uids = []
::Timeout.timeout(timeout) do
@log.debug("start Timeout block for #{timeout} seconds")
loop do
begin
imap = connect
msgs = imap.search(search_terms)
@log.debug("found message numbers: #{msgs}")
if (msgs && msgs.length > 0)
email_uids = msgs.inject([]) { |email_uids, email_id| email_uids << imap.fetch(email_id, 'UID').last.attr['UID'] }
end
rescue => e
@log.info("Error connecting to IMAP: #{e.message}")
ensure
disconnect(imap) unless imap.nil? end
break unless email_uids.empty?
@log.debug("Couldn't find email yet ... trying again")
sleep 10
end
end
@log.debug("found UIDS: #{email_uids}}")
email_uids
end
|
#find_emails(search_terms, timeout = 60) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/watirmark_email/qamail.rb', line 107
def find_emails(search_terms, timeout = 60)
uids = find_email_uids(search_terms, timeout)
emails = WatirmarkEmail::EmailCollection.new
::Timeout.timeout(timeout) do
@log.debug("start Timeout block for #{timeout} seconds")
loop do
begin
imap = connect
fetchdata = imap.uid_fetch(uids, ["ENVELOPE", "BODY[TEXT]", "BODY[]", "UID"])
emails.add_emails(fetchdata)
rescue => e
@log.info("#{e.class}: #{e.message}")
ensure
disconnect(imap) unless imap.nil? end
break unless emails.empty?
@log.debug("Couldn't find email yet ... trying again")
sleep 10
end
end
emails
end
|
#get_email_attachment(search_arry, timeout = 600) ⇒ Object
75
76
77
78
79
|
# File 'lib/watirmark_email/qamail.rb', line 75
def get_email_attachment(search_arry, timeout=600)
@log.debug("Searching for email attachment with query: #{search_arry}")
super search_arry, timeout
end
|
#get_email_replyto(search_array, timeout = 600, delete = true) ⇒ Object
69
70
71
72
73
|
# File 'lib/watirmark_email/qamail.rb', line 69
def get_email_replyto(search_array, timeout=600, delete=true)
@log.debug("Searching for email with query: #{search_array}")
super search_array, timeout, delete
end
|
#get_email_text(search_array, timeout = 600, delete = true) ⇒ Object
This keeps polling the email inbox until a message is found with the given parameters (based on net::IMAP search) or the timeout is reached. This also deletes the email from the inbox if the delete flag is set to true. Returns the email text.
search_array is an array of strings that need to be formatted according to the following convention from Net::IMAP. These strings will be used to send a SEARCH command to search the mailbox for messages that match the given searching criteria:
BEFORE <date>: messages with an internal date strictly before <date>. The date argument has a format similar
to 8-Aug-2002.
BODY <string>: messages that contain <string> within their body.
CC <string>: messages containing <string> in their CC field.
FROM <string>: messages that contain <string> in their FROM field.
NEW: messages with the Recent, but not the Seen, flag set.
NOT <search-key>: negate the following search key.
OR <search-key> <search-key>: "or" two search keys together.
ON <date>: messages with an internal date exactly equal to <date>, which has a format similar to 8-Aug-2002.
SINCE <date>: messages with an internal date on or after <date>.
SUBJECT <string>: messages with <string> in their subject.
TO <string>: messages with <string> in their TO field.
For example:
get_email_text(["SUBJECT", "hello", "NOT", "NEW"])
=> finds emails with the subject "hello" which are not "NEW" (see definition of NEW)
See also: http://tools.ietf.org/html/rfc3501#section-6.4.4
63
64
65
66
67
|
# File 'lib/watirmark_email/qamail.rb', line 63
def get_email_text(search_array, timeout=600, delete=true)
@log.debug("Searching for email with query: #{search_array}")
super search_array, timeout, delete
end
|
#send_email(to, opts = {}) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/watirmark_email/qamail.rb', line 81
def send_email(to, opts={})
opts[:server] ||= 'qasendmail.conviocloud.com'
opts[:from] ||= '[email protected]'
opts[:from_alias] ||= 'Watirmark Email'
opts[:subject] ||= "test"
opts[:body] ||= "Watirmark Email test message"
msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}
#{opts[:body]}
END_OF_MESSAGE
response = Net::SMTP.start(opts[:server]) do |smtp|
smtp.send_message msg, opts[:from], to
end
if response && response.status == "250"
return true
else
return false
end
end
|