Custom mail delivery method in Rails 3.*

Rails 3 allows you to specify a custom mail delivery method in addition to the default options smtp, sendmail, test and file. You can do this by specifying the class name of your mail delivery handler in the config file like this:

config.action_mailer.delivery_method = MyCustomDelivery

And the interface your class needs to implement is:

class CustomDelivery
  def deliver!(mail)

How does this work?

Say you have a mailer class for sending out a welcome email:

class Notifier < ActionMailer::Base
  def welcome
    mail(:to => "",
         :from => "",
         :subject => "Testing custom mail delivery methods",
         :body => "Hello World")

When you try to deliver a welcome email, the Mail object constructed in the “welcome” method is sent to the deliver! method of your delivery class.

mail = Notifier.welcome # Construct a mail object
mail.deliver            # Deliver the mail with the delivery method specified in the config

In your deliver! method you will then have full access to the mail object:

class CustomDelivery
  def deliver!(mail)
    puts       #
    puts mail.from     #

Why is this useful?

Consider a rails application like Twitter where different notification emails go out when people start following each other, send direct messages to each other, etc. Testing these emails by writing mailer tests is good. But it will also be nice to view the actual email that will be delivered to the users. To accomplish this we can write a custom SMTP (catch all) delivery class which inherits from the default Mail::SMTP class.

require 'rubygems'
require 'mail'

class CustomSmtpDelivery < ::Mail::SMTP 
  # SMTP configuration (could be possible to pass the settings from the config file)   
  def initialize(values)     
    self.settings = {:address => "",
                     :port => 587,
                     :domain => 'yourdomain',
                     :user_name => "username",
                     :password => "password",
                     :authentication => 'plain',
                     :enable_starttls_auto => true,
  attr_accessor :settings

  def deliver!(mail)
    # Redirect all mail to your inbox
    mail['to'] = ""
    mail['bcc'] = []
    mail['cc'] = []

In this class we first setup our SMTP configuration and in the deliver! method, we are basically redirecting all outgoing email to our personal email by modifying the “to field” and then passing on the modified mail object to the superclass (Mail::SMTP) deliver! method.

View source here:


Testing subdomain sites the easy way

You have two options to test sites that use multiple subdomains:

  1. Add each and every subdomain to your host file
  2. site1.mywebapp.local site2.mywebapp.local

  3. Setup a DNS server (Dnsmasq or Bind)

Both these approaches have their disadvantages. Updating your hosts file can become tedious, especially if your site supports dynamic subdomain creation. And setting up a DNS server takes some effort.

Tim Pope found a simple workaround for this problem. As he mentions here:

“I grabbed and made it a localhost wildcard, pointing * at”

So that means you can now use URLs like or without having to add them to your hosts file.

One issue remains: say my development box is at in my home network and my Droid is at, how would I be able to access the sites on my Droid? One way is to update the hosts file on the Droid, but you need root access for that.

So I grabbed and made it point to my dev machine’s local ip address: * points to Now I can access my development sites from all the devices on my network without having to update the hosts file on each and every one of them.

Using ActiveMerchant with Payflow Pro

Connection parameter’s for Payflow gateway:

  1. VENDOR => The merchant login ID that you created when you registered
  2. USER => User id of the user responsible for processing transactions (Usually same as the VENDOR unless you created additional users and assigned appropriate roles).
  3. PARTNER => From whom did you purchase your account? If from PayPal, then use the “PayPal”
  4. PWD => The password for the user mentioned in step (2)
gateway =
  :user => 'user-name',        #USER
  :login => 'merchant-id',     #VENDOR
  :partner => 'partner-name',  #PARTNER
  :password => 'my-password'   #PWD

Hello world!

def print_hello
  puts "Hello world!"