0
votes

I have ActionMailer configured to send emails through gmail in development mode.

config/development.rb

config.action_mailer.default_url_options = { host: ENV["MY_DOMAIN"] }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: ENV["MY_DOMAIN"],
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["MY_USERNAME"],
  password: ENV["MY_PASSWORD"]
}

I have this set up so my designer can trigger and send test html emails to a designated address for testing in a variety of browsers / devices.

However, in development mode, I'd like to block all outgoing emails that are not being sent to that one designated email address.

I'm looking for something like:

config.action_mailer.perform_deliveries = target_is_designated_email_address?

... but I need someway to inspect the Mail instance, to ensure that it's being sent to the correct address.

any ideas?

thanks!

1

1 Answers

2
votes

Check out the Mail interceptors in the mail gem that ActionMailer uses. You can find out more about them in this railscast on ActionMailer.

The relevant part straight from the railscast:

Create a class to redirect mail.

class DevelopmentMailInterceptor
  def self.delivering_email(message)
    message.subject = "[#{message.to}] #{message.subject}"
    message.to = "[email protected]"
  end
end

Register the interceptor in development mode only:

Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?

Then it will just replace the subject line with "[[email protected]] Some subject" and redirect the message to whomever you want.