Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override ActionMailer's mail to address in development environment

In my development environment, I use a copy of the production database when testing locally. For reasons both for testing and simply for protection against sending out test/dev emails to real users, what's the best way to override the mail-to address when in development mode?

I know I can write logic in each mailer, but I have several and it would be nice to put it all in once place. Can I override the mail() method somehow to make the :to parameter always point at an email address I specify?

like image 330
Calvin Avatar asked Mar 14 '12 21:03

Calvin


People also ask

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.

What is action mailer?

1 Introduction. Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.


2 Answers

I use an ActionMailer interceptor so all mails sent while in development or test environments are sent to the same address. Like this:

# config/initializers/override_mail_recipient.rb
if Rails.env.development? or Rails.env.test?
  class OverrideMailRecipient
    def self.delivering_email(mail)
      mail.to = '[email protected]'
    end
    ActionMailer::Base.register_interceptor(OverrideMailRecipient)
  end
end
like image 62
rilla Avatar answered Oct 22 '22 16:10

rilla


I think the best option is to use a service like mailtrap.io: http://mailtrap.io or mailcatcher gem: https://rubygems.org/gems/mailcatcher

like image 25
jeffersongirao Avatar answered Oct 22 '22 14:10

jeffersongirao