Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'rubygems'
- require 'net/imap'
- require 'net/smtp'
- require 'tmail'
- #Config Variables
- smtpserver='mail.server.com'
- imapserver='mail.server.com'
- iousername='hyl****'
- iopassword='*****'
- mailfrom='fax@sserver.com'
- defdomain='server.com'
- securessl=true
- Net::IMAP.debug = false
- begin
- puts "Starting up"
- #imap connect
- imap = Net::IMAP.new(imapserver,993,securessl,nil,false)
- imap.login(iousername,iopassword)
- rescue => error
- #handle unable to connect/login errors and loop until connected
- loop do
- begin
- imap = Net::IMAP.new(imapserver,993,securessl,nil,false)
- imap.login(iousername,iopassword)
- rescue => error2
- print "Error: #{error2}\n"
- print "Sleeping 30 seconds\n"
- sleep(30)
- end
- if (!error2)
- break
- end
- end
- end
- #daemon loop!~
- loop do
- puts "Checking inbox"
- time = Time.new
- begin
- imap.select("INBOX")
- rescue => e
- #handle unable to select inbox errors (lost connection), try to reconnect and loop until no errors
- print "ERROR: #{e}\n"
- loop do
- begin
- imap = Net::IMAP.new(imapserver)
- imap.login(iousername,iopassword)
- imap.select("INBOX")
- rescue => ee
- print "Error: #{ee}\n"
- end
- if (!ee)
- break
- end
- sleep(15)
- end
- end
- ## Serach the IMAP box for a email from begining of time
- imap.search(["SINCE", "20-APR-2009"]).each do |id|
- email = imap.fetch(id, "BODY[]")[0].attr["BODY[]"]
- email = TMail::Mail.parse(email)
- env = imap.fetch(id, "ENVELOPE")[0].attr["ENVELOPE"]
- efrom = "#{env.from[0].mailbox}@#{env.from[0].host}"
- subject = email.subject
- ## Now we parse the subject and pull out phone number form it
- tmp = subject[subject.index('fax:')+4..subject.length]
- faxNumber = tmp.gsub(/\D/,'')
- #get attachement and write it to a file in /tmp
- #delete message
- imap.store(id, "+FLAGS", [:Deleted])
- imap.expunge
- end
- sleep(15)
- end
Add Comment
Please, Sign In to add comment