Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict ActionMailer to one domain

I have a rails application running in a staging-environment, which is an accurate copy of the production. I would like to be able to send mails with ActionMailer, to test that it all works as it should, but to prevent any mistakes, I would very much like to be able to restrict the mailer from sending out to any addresses that are not on my own domain.

It doesn't seem that ActionMailer supports this from the get go, but is there a plugin or perhaps a patch of some sort, that could do this?

like image 545
troelskn Avatar asked Oct 26 '22 09:10

troelskn


2 Answers

I am doing following with success:

module FilteredMailer

  def self.included(base)
    base.class_eval do
      alias_method :create_mail_orig, :create_mail

      def create_mail
        recipients(filter_out_recipients(recipients))
        create_mail_orig
      end
    end
  end

  private

  def filter_out_recipients(recipients)
    ...
  end
end

You need to include this module in all your mailers. This works in Rails 2.3.8, I don't know if it works in Rails 3.

like image 155
skalee Avatar answered Nov 09 '22 08:11

skalee


Take a look at the sanitize_email gem - it's a fine solution for this.

It doesn't let you specify a domain, but it does allow you to restrict email sending to a specific list of recipients which will prevent any emails going to unintended recipients.

like image 29
Olly Avatar answered Nov 09 '22 09:11

Olly