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

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Geoffrey Grosenbach Tue, 15 Apr 2008 08:26:16 PDT

    Note: GMail limits you to 500 outgoing messages per day. I’m using the AuthSMTP to send email without those limits (even though the main email account is hosted at GMail).

  2. Mike Tue, 22 Apr 2008 03:01:21 PDT

    Nice work!

  3. Luke Francl Tue, 22 Apr 2008 12:14:43 PDT

    The most recent version of ar_mailer (I’m using 1.3.1) already includes the SMTP TLS code, so I do not believe installing action_mailer_tls is necessary. I could be wrong, though, since I already had it installed when I installed ar_mailer. ;)

  4. David Baldwin Tue, 17 Mar 2009 17:35:41 PDT

    Thanks for the post. I am currently setting up ar_mailer with authsmtp. It is currently only working when I enable SSL on my authsmtp account. I would rather not have that enabled as SSL messages count as double. How do I explicitly tell smtp not to use SSL in the Rails settings?

  5. David Baldwin Tue, 17 Mar 2009 17:51:08 PDT

    BTW - Here are my SMTP settings. Note that I am using the most recent version of the ar_mailer gem (http://github.com/adzap/ar_mailer/)

    config.action_mailer.delivery_method = :activerecord

    config.action_mailer.smtp_settings = {
    :address => “mail.authsmtp.com”,
    :port => 25,
    :user_name => “my_username”,
    :password => “my_password”,
    :domain => “localhost”,
    :authentication => :login,
    :tls => false
    }

  6. Daehee Mon, 08 Jun 2009 07:39:39 PDT

    David — thanks for the SMTP settings to get AuthSMTP working with ar_mailer!

  7. Jack Sat, 28 Nov 2009 09:04:57 PST

    Here’s a good alternative to ar_mailer:
    http://www.postageapp.com
    It’s easy to start and you don’t have to deal with background jobs and
    queues plus you have some cool reporting tools - great to track what
    actually got sent.

    Here’s an introduction:

    http://blog.postageapp.com/2009/11/easy-mass-mailing-for-ruby-on-rails/

    And here’s a sample app to help get started:

    http://blog.postageapp.com/2009/11/rails-example-app/

  8. Will Fri, 19 Mar 2010 08:35:04 PDT

    Jack, You seem to spam every single site talking about rails mailing lists. You might want to mention that your solution is free only for 500 messages per day. I would imagine most sites using rails have newsletters far greater than 500 users. At least mention that before you tell everyone you have an end all solution.

Comments