Using Gmail with IMAP to Receive Email in Rails (cron job version)
This pretty much comes about because of RailsTips and Peepcode. Thank you guys.
Goal
You want your user to be able to send an email and have that email get posted as a comment to the rails app.
Requirements
1. Your rails app should already have the ability to send out emails
2. Your Rails app must already be capified via Capistrano or similar with some type of deployment recipe
3. You need an additional gmail or google apps account with imap enabled
4. You need the ability to run cron jobs on your server
Configuration
First, setup the cron task and make it easy to run with cap deploy:start and cap deploy:stop
Create file lib/tasks/chores.rake
Create file config/cron.erb
Add the following near the end of your deploy.rb file
Second, setup the login information for the gmail mail account we will use to receive emails through.
Create file config/mail.yml and configure for imap
Install lockfile and tmail cause your gonna need them:
sudo gem install lockfile
sudo gem install tmail
Edit environment.rb
Retrieve the email and save to the database
Edit lib/tasks/chores.rake and put the following code inside the each_minute task. (credit for this goes to John of RailsTips.org though I added a quick fix to handle multipart emails)
Deployment
Deploy your app as usual with cap deploy
Next, run cap cron:start to start up the cron task. This will run every minute.
Send an email to the account you specified in mail.rb and after one minute you should see the email saved as a comment on the app.
