Mail Interceptor to Stop Sending Mails From Development Environment

Sometimes it can be pretty frustrating to test out application while receiving a lot of emails from the development environment. Its simply easy to stop sending the emails from development env itself.

Create a custom PreventMailInterceptor in lib folder.

prevent_mail_interceptor.rb
1
2
3
4
5
6
7
class PreventMailInterceptor

  def self.delivering_email(message)
      message.perform_deliveries = false if Rails.env.development?
  end

end

delivering_email method defines whether the mail should be delivered or not.

require the file and include it to the ActionMailer::Base class

action_mailer.rb
1
2
require "#{Rails.root.to_s}/lib/prevent_mail_interceptor"
ActionMailer::Base.register_interceptor(PreventMailInterceptor)

Restart the code and enjoy the application with no more mails.

Happy Coding :)

Comments