Guest User

Untitled

a guest
Nov 30th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. require 'gmail'
  2. require 'net/ftp'
  3.  
  4. local_path = "tmp"
  5.  
  6. def ftp_to_bluehost(file)
  7. ftp_user = "user@domain.com"
  8. ftp_pass = "pass"
  9. ftp_host = "ftp.domain.com"
  10. ftp_port = 21
  11. remote_file_path = "remote"
  12.  
  13. ftp = Net::FTP.new
  14. ftp.connect(ftp_host,ftp_port)
  15. ftp.login(ftp_user,ftp_pass)
  16. ftp.mkdir(remote_file_path) if ftp.ls.include?(remote_file_path)
  17. ftp.chdir(remote_file_path)
  18. ftp.putbinaryfile(file)
  19. ftp.close
  20. end
  21.  
  22. def get_bluehost_file(file, local_path)
  23. ftp_user = "user@domain.com"
  24. ftp_pass = "pass"
  25. ftp_host = "ftp.domain.com"
  26. ftp_port = 21
  27. remote_file_path = "remote"
  28.  
  29. ftp = Net::FTP.new
  30. ftp.connect(ftp_host,ftp_port)
  31. ftp.login(ftp_user,ftp_pass)
  32. ftp.chdir(remote_file_path)
  33. ftp.getbinaryfile(file, "tmp/#{file}")
  34.  
  35. ftp.close
  36. end
  37.  
  38. def delete_tmp_dir(local_path)
  39. Dir.foreach(local_path) { |file| puts file; File.delete("#{local_path}/#{file}") unless file == "." || file == ".."}
  40. end
  41.  
  42. #working in 1.9.3
  43. gmail = Gmail.connect('user@gmail.com', 'pass')
  44. gmail.inbox.emails.each do |email|
  45. email.message.attachments.each do |f|
  46. f.filename.match(/[\w+-_ ]+.(\w{3})/)
  47. puts "extension for #{f.filename} is #{$1}"
  48. File.write(File.join(local_path, f.filename), f.body.decoded)
  49. ftp_to_bluehost(File.join(local_path, f.filename))
  50. delete_tmp_dir(local_path)
  51. get_bluehost_file(f.filename, "#{local_path}/#{f.filename}")
  52. end
  53. end
  54.  
  55. gmail.logout
Add Comment
Please, Sign In to add comment