Override ActionMailer recipient in Rails 2 and 3 for development or testing

If your like me, you might want enable delivering of mail in development, mostly because testing email layout is a pain without actually sending the email to a real client.

In doing this you’ll probably want to avoid the possibility of emails making their way to your customers from your development machine, this can be achieved with a simple initializer to override the destination.

Rails 2.x

1
2
3
4
5
6
7
8
9
10
11
12
if Rails.env.development?
  class ActionMailer::Base
    def create_mail_with_overriding_recipients
      mail = create_mail_without_overriding_recipients
      mail.to = "mail@robaldred.co.uk"
      #mail.cc =
      #mail.bcc =
      mail
    end
    alias_method_chain :create_mail, :overriding_recipients
  end
end

Rails 3.x

1
2
3
4
5
6
7
8
if Rails.env.development?
  class OverrideMailReciptient
    def self.delivering_email(mail)
      mail.to = "mail@robaldred.co.uk"
    end
  end
  ActionMailer::Base.register_interceptor(OverrideMailReciptient)
end

If you prefer to use a plugin, there is one called mail_safe personally, i think it’s a little overkill.

2 thoughts on “Override ActionMailer recipient in Rails 2 and 3 for development or testing

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>