<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;p&gt;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.&lt;/p&gt;
script/generate authenticated_mailer user
&lt;p&gt;The mailer uses an Observer to know when to send out notifications. Once generated, you will have to add this to your controller:&lt;/p&gt;
class AccountController &amp;lt; ActionController::Base
observer :user_observer
end
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;NOTE&lt;/span&gt; &#8211; 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:&lt;/p&gt;
&lt;p&gt;config.active_record.observers = :user_observer&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Here are a few controller tests for the mailer:&lt;/p&gt;
&lt;p&gt;class AccountControllerTest &amp;lt; Test::Unit::TestCase&lt;/p&gt;
def setup
@controller = AccountController.new
@request    = ActionController::TestRequest.new
@response   = ActionController::TestResponse.new
&lt;ol&gt;
	&lt;li&gt;for testing action mailer&lt;br /&gt;
    ActionMailer::Base.delivery_method = :test&lt;br /&gt;
    ActionMailer::Base.perform_deliveries = true&lt;br /&gt;
    ActionMailer::Base.deliveries = []&lt;br /&gt;
    @emails = ActionMailer::Base.deliveries &lt;br /&gt;
    @emails.clear&lt;br /&gt;
  end&lt;/li&gt;
&lt;/ol&gt;
def test_should_activate_user_and_send_activation_email
get :activate, :id =&amp;gt; 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 =&amp;gt; { :login =&amp;gt; &amp;#8216;quire&amp;#8217;, :email =&amp;gt; &amp;#8216;quire@example.com&amp;#8217;,
:password =&amp;gt; &amp;#8216;quire&amp;#8217;, :password_confirmation =&amp;gt; &amp;#8216;quire&amp;#8217; }.merge(options)
end
&lt;p&gt;end&lt;/p&gt;
&lt;p&gt;Change the file models/user_notifier.rb&lt;/p&gt;
&lt;p&gt;You will also want to enter a real email or else mail will not be processed by yahoo or google etc.&lt;/p&gt;
protected
def setup_email(user)
@recipients  = &amp;#8220;#{user.email}&amp;#8221;
@from        = &amp;#8220;&lt;span class=&quot;caps&quot;&gt;ADMINEMAIL&lt;/span&gt;&amp;#8221;
@subject     = &amp;quot;[&lt;span class=&quot;caps&quot;&gt;YOURSITE&lt;/span&gt;] &amp;quot;
@sent_on     = Time.now
@body[:user] = user
end
&lt;p&gt;to&lt;/p&gt;
protected
def setup_email(user)
@recipients  = &amp;#8220;#{user.email}&amp;#8221;
&lt;code&gt;from        = &quot;any.email&lt;/code&gt;mydomain.com&amp;quot;
@subject     = &amp;#8220;Welcome to my fantastic website.&amp;#8221;
@sent_on     = Time.now
@body[:user] = user
end
&lt;p&gt;Questions&lt;/p&gt;
&lt;p&gt;Question: Rails 2.0 doesn&#8217;t support Observers. What changes we need to do?&lt;/p&gt;
&lt;p&gt;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)&lt;/p&gt;
&lt;p&gt;Question: I&#8217;m getting an error from the controller:&lt;/p&gt;
&lt;p&gt;&amp;gt; Due to changes in Action Mailer?, you need to provide the mailer_name along with the template name.&lt;/p&gt;
&lt;p&gt;When I comment out this line in config/environment.rb:&lt;/p&gt;
&lt;p&gt;&amp;gt; # RAILS_GEM_VERSION = &#8216;2.0.2&#8217; unless defined? RAILS_GEM_VERSION&lt;/p&gt;
&lt;p&gt;the error goes away. Can anyone offer any advice? Many Thanks.&lt;br /&gt;
Note&lt;/p&gt;
&lt;p&gt;Note: Aha! You also need to configure the environment files (e.g environments/development.rb) with something like:&lt;/p&gt;
&lt;p&gt;ActionMailer::Base.smtp_settings = {&lt;br /&gt;
  :address  =&amp;gt; &amp;#8220;your smtp server&amp;#8221;,&lt;br /&gt;
  :port  =&amp;gt; smtp-port, &lt;br /&gt;
  :domain  =&amp;gt; &amp;#8216;your domain&amp;#8217;&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;Note&lt;/p&gt;
&lt;p&gt;Note: If you are using email notification, it would be good to add something like the code below to your user.rb&lt;/p&gt;
&lt;p&gt;validates_format_of :email, :with =&amp;gt; /(^([^&lt;code&gt;\s]+)&lt;/code&gt;((?:[-_a-z0-9]&lt;ins&gt;\.)&lt;/ins&gt;[a-z]{2,})$)|(^$)/i&lt;/p&gt;
&lt;p&gt;Note&lt;/p&gt;
&lt;p&gt;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?&lt;br /&gt;
Note&lt;/p&gt;
&lt;p&gt;I am running Rails v 1.2.3 and have the same problem. The observer worked fine in environment.rb before 1.2.3.&lt;br /&gt;
tags: actsasauthenticated&lt;br /&gt;
Revised 18 days ago by Anonymous (149.155.191.102)&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Edit Page&lt;/li&gt;
	&lt;li&gt;Back in Time (16 revisions &#8212; 4 authors )&lt;/li&gt;
	&lt;li&gt;See Changes Hide Changes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Send a link to this page&amp;#8230;&lt;br /&gt;
From&amp;#8230;&lt;/p&gt;
&lt;p&gt;to&amp;#8230;&lt;/p&gt;
&lt;p&gt;Attach a note?&lt;/p&gt;
&lt;p&gt;Don&amp;#8217;t worry, we don&amp;#8217;t save e-mail addresses anywhere except for authorship purposes.&lt;/p&gt;</body>
  <created-at type="datetime">2008-10-24T03:55:05-07:00</created-at>
  <id type="integer">71698</id>
  <permalink>mailer-setup</permalink>
  <repository-id type="integer">67186</repository-id>
  <title>Mailer Setup</title>
  <updated-at type="datetime">2008-10-24T03:55:05-07:00</updated-at>
  <user-id type="integer">30799</user-id>
</wiki>
