Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup a mail interceptor in rails 3.0.3?

I am using rails 3.0.3, ruby 1.9.2-p180, mail (2.2.13). I m trying to setup a mail interceptor but I am getting the following error

 /home/abhimanyu/Aptana_Studio_3_Workspace/delivery_health_dashboard_03/config/initializers/mailer_config.rb:16:in `<top (required)>': uninitialized constant DevelopmentMailInterceptor (NameError)

How do i fix it?

The code I am using is shown below:

config/initializer/mailer_config.rb

ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "secure.emailsrvr.com",
:port => '25',
:domain => "domain",
:user_name => "user_name",
:password => "password",
:authentication => :plain

}

ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if  Rails.env.development?

lib/development_mail_interceptor.rb

class DevelopmentMailInterceptor

  def self.delivering_email(message)
    message.to = "email"
  end

end

Thanks in advance.

like image 531
Abhimanyu Avatar asked Jun 01 '11 06:06

Abhimanyu


People also ask

How do I use SMTP in Ruby on Rails?

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.

How do I Preview mailers in Rails?

Then the preview will be available in http://localhost:3000/rails/mailers/user_mailer/welcome_email. If you change something in app/views/user_mailer/welcome_email. html. erb or the mailer itself, it'll automatically reload and render it so you can visually see the new style instantly.

What generates information on the mailing run if available?

rb, production. rb, etc...) Generates information on the mailing run if available.


2 Answers

require 'development_mail_interceptor' #add this line ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if  Rails.env.development? 
like image 56
Zabba Avatar answered Sep 21 '22 05:09

Zabba


I found it easier to install the mailcatcher gem. Then in development.rb:

  config.action_mailer.delivery_method = :smtp   config.action_mailer.smtp_settings = {     :address              => "`localhost`",     :port                 => 1025   } 

Then just run "mailcatcher" and hit http://localhost:1080/ in a browser. It runs in the background, but can be quit directly from the browser. Gives you text+html views, source, and analysis with fractal, if you swing that way. Super-clean.

like image 41
shacker Avatar answered Sep 25 '22 05:09

shacker