Class: WatirmarkEmail::Gmail
- Inherits:
-
BaseController
- Object
- BaseController
- WatirmarkEmail::Gmail
- Defined in:
- lib/watirmark_email/gmail.rb
Constant Summary collapse
- URL =
"imap.gmail.com"- PORT =
993- MAILBOX_INBOX =
"INBOX"- MAILBOX_TRASH =
"[Gmail]/Trash"- MAILBOX_ALL =
"[Gmail]/All Mail"
Instance Attribute Summary collapse
-
#inbox ⇒ Object
Returns the value of attribute inbox.
Instance Method Summary collapse
- #delete(email_uid, imap) ⇒ Object
-
#get_email_text(search_array, timeout = 600, delete = true, since_sec = 3600) ⇒ 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(email, password, logLevel = ::Logger::INFO) ⇒ Gmail
constructor
Constructor for this class.
- #send_email(to, opts = {}) ⇒ Object
Methods inherited from BaseController
#connect, #copy, #disconnect, #get_email_attachment, #get_email_replyto
Constructor Details
#initialize(email, password, logLevel = ::Logger::INFO) ⇒ Gmail
Constructor for this class. This will initialize all variables according to the type email service this is using.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/watirmark_email/gmail.rb', line 12 def initialize(email, password, logLevel = ::Logger::INFO) @email = email @password = password @log = ::Logger.new STDOUT @log.level = logLevel @url = URL @port = PORT @inbox = MAILBOX_INBOX @trash = MAILBOX_TRASH @ssl = true # port 993 end |
Instance Attribute Details
#inbox ⇒ Object
Returns the value of attribute inbox.
3 4 5 |
# File 'lib/watirmark_email/gmail.rb', line 3 def inbox @inbox end |
Instance Method Details
#delete(email_uid, imap) ⇒ Object
24 25 26 27 |
# File 'lib/watirmark_email/gmail.rb', line 24 def delete(email_uid, imap) imap.uid_copy(email_uid, @trash) imap.uid_store(email_uid, "+FLAGS", [:Deleted]) end |
#get_email_text(search_array, timeout = 600, delete = true, since_sec = 3600) ⇒ 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>: with an internal date strictly before <date>. The date argument has a format similar
to 8-Aug-2002.
BODY <string>: that contain <string> within their body.
CC <string>: containing <string> in their CC field.
FROM <string>: that contain <string> in their FROM field.
NEW: 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>: with an internal date exactly equal to <date>, which has a format similar to 8-Aug-2002.
SINCE <date>: with an internal date on or after <date>.
SUBJECT <string>: with <string> in their subject.
TO <string>: 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
57 58 59 60 61 62 63 64 |
# File 'lib/watirmark_email/gmail.rb', line 57 def get_email_text(search_array, timeout=600, delete=true, since_sec=3600) # Only look for emails that have come in since the last hour since = Time.now - since_sec imap_search_terms = search_array.dup.push("SINCE", since.strftime('%d-%b-%Y')) @log.debug("Searching for email with query: #{imap_search_terms}") super imap_search_terms, timeout, delete, since_sec end |
#send_email(to, opts = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/watirmark_email/gmail.rb', line 66 def send_email(to, opts={}) opts[:from] ||= '[email protected]' opts[:from_alias] ||= 'Watirmark Email' opts[:subject] ||= "test" opts[:body] ||= "Watirmark Email test message" smtp = Net::SMTP.new 'smtp.gmail.com', 587 smtp.enable_starttls msg = "From: \#{opts[:from_alias]} <\#{opts[:from]}>\nTo: <\#{to}>\nSubject: \#{opts[:subject]}\n\n \#{opts[:body]}\n" response = smtp.start('smtp.gmail.com', @email, @password, :plain) do |smpt| smtp. msg, opts[:from], to end if response && response.status == "250" return true else return false end end |