Skip to content
This repository was archived by the owner on Oct 14, 2026. It is now read-only.

Mailer Setup

Pticed edited this page Sep 13, 2010 · 2 revisions

Once your authenticated model is set up, you might want to set up email notifications. Acts as Authenticated comes with some basic mailers for this task.

  script/generate authenticated_mailer user

The mailer uses an Observer to know when to send out notifications. Once generated, you will have to add this to your controller:

  class AccountController < ActionController::Base
    observer :user_observer
  end

NOTE – The observer method is deprecated in Rails 1.2 and will be removed in 2.0. If you are using 1.2 or later do not add the above code to your AccountController. Instead, in the Rails::Initializer.run do |config| block of config/environment.rb add the following line:

config.active_record.observers = :user_observer

Now, because you typically want to send notification emails because your application requires some kind of activation, the generated mailer and observer assume you have the User Activation stuff set up.

Here are a few controller tests for the mailer:

class AccountControllerTest < Test::Unit::TestCase

  def setup
    @controller = AccountController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    # for testing action mailer
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    @emails = ActionMailer::Base.deliveries 
    @emails.clear
  end

  def test_should_activate_user_and_send_activation_email
    get :activate, :id => users(:arthur).activation_code
    assert_equal 1, @emails.length
    assert(@emails.first.subject =~ /Your account has been activated/)
    assert(@emails.first.body    =~ /#{assigns(:user).login}, your account has been activated/)
  end

  def test_should_send_activation_email_after_signup
    create_user
    assert_equal 1, @emails.length
    assert(@emails.first.subject =~ /Please activate your new account/)
    assert(@emails.first.body    =~ /Username: quire/)
    assert(@emails.first.body    =~ /Password: quire/)
    assert(@emails.first.body    =~ /account\/activate\/#{assigns(:user).activation_code}/)
  end

  protected
  def create_user(options = {})
    post :signup, :user => { :login => 'quire', :email => 'quire@example.com', 
                             :password => 'quire', :password_confirmation => 'quire' }.merge(options)
  end
end

Change the file models/user_notifier.rb

You will also want to enter a real email or else mail will not be processed by yahoo or google etc.

  protected
  def setup_email(user)
    @recipients  = "#{user.email}" 
    @from        = "ADMINEMAIL" 
    @subject     = "[YOURSITE] " 
    @sent_on     = Time.now
    @body[:user] = user
  end

to

  protected
  def setup_email(user)
    @recipients  = "#{user.email}" 
    @from        = "any.email@mydomain.com" 
    @subject     = "Welcome to my fantastic website." 
    @sent_on     = Time.now
    @body[:user] = user
  end

Questions

Question: Rails 2.0 doesn’t support Observers. What changes we need to do?

Answer: I believe that Rails 2.0 does support Observers, however the observer method in the controller is deprecated. Instead follow the directions above for how to add the observer in environment.rb. (Patrick Joyce 4/28/2007)

Question: I’m getting an error from the controller:

> Due to changes in Action Mailer?, you need to provide the mailer_name along with the template name.

When I comment out this line in config/environment.rb:

> # RAILS_GEM_VERSION = ‘2.0.2’ unless defined? RAILS_GEM_VERSION

the error goes away. Can anyone offer any advice? Many Thanks.
Note

Note: Aha! You also need to configure the environment files (e.g environments/development.rb) with something like:

ActionMailer::Base.smtp_settings = {
  :address  => "your smtp server",
  :port  => smtp-port, 
  :domain  => 'your domain'
} 

Note

Note: If you are using email notification, it would be good to add something like the code below to your user.rb

validates_format_of :email, :with => /(^([^@\s]+)@((?:[-_a-z0-9]+\.)+[a-z]{2,})$)|(^$)/i

Note

I am running Rails v 1.2.3, and I was only able to get the observer to work when placing the observer method in the controller. Has anyone else experienced this?
Note

I am running Rails v 1.2.3 and have the same problem. The observer worked fine in environment.rb before 1.2.3.
tags: actsasauthenticated
Revised 18 days ago by Anonymous (149.155.191.102)

  • Edit Page
  • Back in Time (16 revisions — 4 authors )
  • See Changes Hide Changes

Send a link to this page…
From…

to…

Attach a note?

Don’t worry, we don’t save e-mail addresses anywhere except for authorship purposes.

Clone this wiki locally