Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # default rails environment to development
- ENV['RAILS_ENV'] ||= 'development'
- # require rails environment file which basically "boots" up rails for this script
- require File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')
- require 'net/imap'
- require 'net/http'
- # mail.yml is the imap config for the email account (ie: username, host, etc.)
- config = YAML.load(File.read(File.join(RAILS_ROOT, 'config', 'mail.yml')))
- # make a connection to imap account
- imap = Net::IMAP.new(config['host'], config['port'], true)
- imap.login(config['username'], config['password'])
- # select inbox as our mailbox to process
- imap.select('Inbox')
- # get all emails that are in inbox that have not been deleted
- imap.uid_search(["NOT", "DELETED"]).each do |uid|
- # fetches the straight up source of the email for tmail to parse
- source = imap.uid_fetch(uid, ['RFC822']).first.attr['RFC822']
- # comment = Comment.new_from_email(source)
- mail = TMail::Mail.parse(source)
- puts mail.to
- puts mail.from
- puts mail.subject
- puts mail.body #.split("\n\n").first
- # 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
- # expunge removes the deleted emails
- imap.expunge
- imap.logout
- imap.disconnect
Add Comment
Please, Sign In to add comment