Skip to content

Instantly share code, notes, and snippets.

@motdotla
Created November 19, 2008 01:30
Show Gist options
  • Save motdotla/26367 to your computer and use it in GitHub Desktop.
Save motdotla/26367 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment