Guest User

Untitled

a guest
Jul 16th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. require 'rubygems'
  3. require 'net/imap'
  4. require 'net/smtp'
  5. require 'tmail'
  6.  
  7. #Config Variables
  8. smtpserver='mail.server.com'
  9. imapserver='mail.server.com'
  10. iousername='hyl****'
  11. iopassword='*****'
  12. mailfrom='fax@sserver.com'
  13. defdomain='server.com'
  14. securessl=true
  15. Net::IMAP.debug = false
  16.  
  17. begin
  18. puts "Starting up"
  19. #imap connect
  20. imap = Net::IMAP.new(imapserver,993,securessl,nil,false)
  21. imap.login(iousername,iopassword)
  22. rescue => error
  23. #handle unable to connect/login errors and loop until connected
  24. loop do
  25. begin
  26. imap = Net::IMAP.new(imapserver,993,securessl,nil,false)
  27. imap.login(iousername,iopassword)
  28. rescue => error2
  29. print "Error: #{error2}\n"
  30. print "Sleeping 30 seconds\n"
  31. sleep(30)
  32. end
  33. if (!error2)
  34. break
  35. end
  36. end
  37. end
  38.  
  39. #daemon loop!~
  40. loop do
  41. puts "Checking inbox"
  42. time = Time.new
  43. begin
  44. imap.select("INBOX")
  45. rescue => e
  46. #handle unable to select inbox errors (lost connection), try to reconnect and loop until no errors
  47. print "ERROR: #{e}\n"
  48. loop do
  49. begin
  50. imap = Net::IMAP.new(imapserver)
  51. imap.login(iousername,iopassword)
  52. imap.select("INBOX")
  53. rescue => ee
  54. print "Error: #{ee}\n"
  55. end
  56. if (!ee)
  57. break
  58. end
  59. sleep(15)
  60. end
  61. end
  62. ## Serach the IMAP box for a email from begining of time
  63. imap.search(["SINCE", "20-APR-2009"]).each do |id|
  64. email = imap.fetch(id, "BODY[]")[0].attr["BODY[]"]
  65. email = TMail::Mail.parse(email)
  66. env = imap.fetch(id, "ENVELOPE")[0].attr["ENVELOPE"]
  67. efrom = "#{env.from[0].mailbox}@#{env.from[0].host}"
  68. subject = email.subject
  69.  
  70. ## Now we parse the subject and pull out phone number form it
  71. tmp = subject[subject.index('fax:')+4..subject.length]
  72. faxNumber = tmp.gsub(/\D/,'')
  73. #get attachement and write it to a file in /tmp
  74.  
  75. #delete message
  76. imap.store(id, "+FLAGS", [:Deleted])
  77. imap.expunge
  78. end
  79. sleep(15)
  80. end
Add Comment
Please, Sign In to add comment