Batch Mailing in Rails
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.



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).
Nice work!
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.
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?
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
}
David — thanks for the SMTP settings to get AuthSMTP working with ar_mailer!
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/
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.