fmcxjw
Last Updated: February 25, 2016
·
1.121K
· estevaoam
C33f5ce483932bffd2ea5b9e29d55dcc

How to intercept every email when developing using production data

If you have the habit to use production data to test your application locally you possibly have deactivated the mailer to avoid sending test emails for your users.

Here's a cool trick that intercepts any mail and sets the "to" param to your email address.

Create a file in config/initializers/ with this content:

if Rails.env.development?
  class DevelopmentInterceptor
    def self.delivering_email(message)
      message.to  = "\"#{message.to.first}\" <youremail@gmail.com>"
      message.cc, message.bcc = nil, nil
    end 
  end 

  ActionMailer::Base.register_interceptor(DevelopmentInterceptor)
end

Don't forget to replace with your email address and that's it, it'll send everything to your email address when in the development environment.

Say Thanks
Respond

1 Response
Add your response

8611
Cb01841c0ba3d7c0a5d46a6ef9f329f7

Very cool.

I've used Ryan's letteropener gem too, and it works great:
https://github.com/ryanb/letter
opener

over 1 year ago ·