Created
January 9, 2012 21:21
-
-
Save scottraio/1584997 to your computer and use it in GitHub Desktop.
E-mail polling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/imap' | |
require 'net/http' | |
EMAIL = "[email protected]" | |
NAME = "Workory Support" | |
class Gmail | |
def initialize(username,password) | |
@imap = Net::IMAP.new("imap.gmail.com", 993, true) | |
@imap.login(username, password) | |
# select inbox as our mailbox to process | |
@imap.select('Inbox') | |
return @imap | |
end | |
def self.inbox(username,password) | |
self.new(username,password) | |
end | |
def emails | |
uids = @imap.uid_search(["NOT", "DELETED"]) | |
puts "No messages found on server" if uids.nil? or uids.empty? | |
messages = [] | |
for uid in uids | |
messages << TMail::Mail.parse(@imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822']) | |
# there isn't move in imap so we copy to new mailbox and then delete from inbox | |
@imap.uid_copy(uid, "[Gmail]/All Mail") | |
@imap.uid_store(uid, "+FLAGS", [:Deleted]) | |
end | |
return messages | |
end | |
def close | |
@imap.logout | |
@imap.disconnect | |
end | |
def body(email) | |
message_id = email.message_id('') | |
x_mailer = email.header_string('x-mailer') | |
body = "No message." | |
if email.multipart? | |
puts "Message is mutipart...parsing..." | |
email.parts.each do |subpart| | |
body = subpart.body if subpart.content_type == 'text/plain' | |
end | |
else | |
body = email.body | |
end | |
# For optimization, this list could be sorted from most popular to least popular email client/service | |
rules = [ | |
[ 'Gmail', lambda { message_id =~ /.+gmail\.com>\z/}, /^.*#{NAME}\s+<#{EMAIL}>\s*wrote:.*$/ ], | |
[ 'Yahoo! Mail', lambda { message_id =~ /.+yahoo\.com>\z/}, /^_+\nFrom: #{NAME} <#{EMAIL}>$/ ], | |
[ 'Microsoft Live Mail/Hotmail', lambda { email.header_string('return-path') =~ /<.+@(hotmail|live).com>/}, /^Date:.+\nSubject:.+\nFrom: #{EMAIL}$/ ], | |
[ 'Outlook Express', lambda { x_mailer =~ /Microsoft Outlook Express/ }, /^----- Original Message -----$/ ], | |
[ 'Outlook', lambda { x_mailer =~ /Microsoft Office Outlook/ }, /^\s*_+\s*\nFrom: #{EMAIL}.*$/ ], | |
# TODO: other email clients/services | |
# Generic fallback | |
[ nil, lambda { true }, /^.*#{EMAIL}.*$/ ] | |
] | |
# Default to using the whole body as the reply (maybe the user deleted the original message when they replied?) | |
notes = body | |
source = nil | |
# Try to detect which email service/client sent this message | |
rules.find do |r| | |
if r[1].call | |
# Try to extract the reply. If we find it, save it and cancel the search. | |
reply_match = email.body.match(r[2]) | |
if reply_match | |
notes = body[0, reply_match.begin(0)] | |
source = r[0] | |
next true | |
end | |
end | |
end | |
notes.strip | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment