Guest User

Untitled

a guest
Feb 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. # default rails environment to development
  2. ENV['RAILS_ENV'] ||= 'development'
  3. # require rails environment file which basically "boots" up rails for this script
  4. require File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')
  5. require 'net/imap'
  6. require 'net/http'
  7.  
  8. # mail.yml is the imap config for the email account (ie: username, host, etc.)
  9. config = YAML.load(File.read(File.join(RAILS_ROOT, 'config', 'mail.yml')))
  10.  
  11. # make a connection to imap account
  12. imap = Net::IMAP.new(config['host'], config['port'], true)
  13. imap.login(config['username'], config['password'])
  14. # select inbox as our mailbox to process
  15. imap.select('Inbox')
  16.  
  17. # get all emails that are in inbox that have not been deleted
  18. imap.uid_search(["NOT", "DELETED"]).each do |uid|
  19. # fetches the straight up source of the email for tmail to parse
  20. source = imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822']
  21. # comment = Comment.new_from_email(source)
  22.  
  23. mail = TMail::Mail.parse(source)
  24. puts mail.to
  25. puts mail.from
  26. puts mail.subject
  27. puts mail.body #.split("\n\n").first
  28.  
  29. # there isn't move in imap so we copy to new mailbox and then delete from inbox
  30. imap.uid_copy(uid, "[Gmail]/All Mail")
  31. imap.uid_store(uid, "+FLAGS", [:Deleted])
  32. end
  33.  
  34. # expunge removes the deleted emails
  35. imap.expunge
  36. imap.logout
  37. imap.disconnect
Add Comment
Please, Sign In to add comment