I recently had to write something to fetch emails from a GMail account and process the emails to check if the email was a bounce (bad email address). If so, the person in the system associated with the bounced email would be deleted from the database. I did some searching for such thing and found this:
How to receive emails with Rails
About half way down the page you see an example for polling emails from a POP3 server (edited for space):
require 'net/pop'
require File.dirname(__FILE__) + '/../config/environment'
logger = RAILS_DEFAULT_LOGGER
logger.info "Running Mail Importer..."
Net::POP3.start("mail.server.net", nil, "username", "password") do |pop|
if pop.mails.empty?
logger.info "NO MAIL"
else
pop.mails.each do |email|
begin
logger.info "receiving mail..."
Mailman.receive(TMail::Mail.parse(email.pop))
email.delete
rescue Exception => e
logger.error e.message
end
end
end
end
logger.info "Finished Mail Importer."
Continue Reading…
Posted by Steve on Feb 17, 2008
Recently I came across a dilemma in trying to send and receive emails from a Rails app: Either install a full-blown postfix server on a Linux box (by full-blown, I mean account handling, POP/IMAP interface, anti-spam, anti-virus, etc etc), or use Google to handle all the emailing. So, me not being a Linux/sys-admin junkie, I strongly opted for the Google route (and after some persuasion the customer, thankfully, agreed :-). So I went about to find the best practices for working with Google Apps (http://www.google.com/a which is where you sign up). I came across this very helpful post.
You just install the mentioned plugin, create the appropriate mailer.yml file and you’re good to go. ActionMailer will login as the specified account and send emails through that account. Genius!
On a side note, I was able to get a Rails app to talk (no sending though, just receiving) to gmail by using the new net/pop library in the latest ruby and making the Mail Fetcher plugin.
But back to the main point, being able to send through the Google Apps account. The technical stuff seems really easy now. Just send your emails out like normal and have some way to check responses either manually or programmatically like I showed above. You’re golden, or so it seems.
Continue Reading…
Posted by Steve on Feb 11, 2008