Batch Mailing in Rails

Posted by anup.narkhede on April 15, 2008

Recently I worked on a Mass Mailing system for a Rails application using ar_mailer. This tutorial is just an effort to consolidate tips needed to get the mailing system ready and save time on some commonly known problems.
I wanted to implement this for a newsletter application, where a mail needs to be sent to hundreds of recipients for delivery. One of the good choice is to store these mails in a database queue and allow a background process ‘ar_sendmail’ to deliver them one by one.

1. Install ARMailer

Follow this tutorial : http://blog.segment7.net/articles/2006/08/15/ar_mailer.

One of the commonly faced error:

Solution:
Verify that Email model, migration and emails table are created and configured properly.

2. Configuration

Add following lines in environment.rb file

3. Create Mailers

I wanted to have two type of Mailers, one for immediate delivery (alert messages, signup notifications etc) and other for batch delivery (newsletters for mass mailing, which are scheduled to deliver on a particular time). QuickMailer class is an Actionmailer for instant delivery using smtp, and BatchMailer uses ar_mailer to queue the mails in database for delivery using ar_sendmail process.

Note the statement ActionMailer::Base.delivery_method = :smtp at the top, which forces UserMailer to send mail using smtp directly instead of calling ARMailer’s perform_delivery_activerecord(mail).

4. Using Gmail SMTP server

I have used local smtp server to send mails, to avoid authentication for each call and save time. However here are the steps to configure Gmail smtp server.
http://ruby.dzone.com/news/rails-gmail-simple-email-deliv

Install plugin action_mailer_tls from https://openrain.com/opensource/public/rails/plugins/action_mailer_tls

Configure your environment.rb

5. Test