[Rails] ActionMailer

Christian Metts mintxian.list at gmail.com
Sat Jan 8 02:00:24 GMT 2005


On Fri, 7 Jan 2005 16:33:57 +0100, Nicholas Wieland
<nicholas_wieland at yahoo.it> wrote:
> Hi all, I have a few problems with ActionMailer.
> First of all, I've used the script/generate mailer Norifier command, and
> it hasn't created the view, just the model: from what I understand it
> should at least create an app/views/notifier/ directory, but I'm not
> sure ...

Yes, and inside that directory you'd make a registration_mail.rhtml 
(the same name as your mailer) that is the body of your template.

You also need to put the the body variables you want available to the
template in a hash. So it would look something like this:

    def registration_mail(to, user, password)
         [...snip...]
        @body[password] = password
        @body[username] = username # this way you can have multiple
variables passed to the template.
    end
> 
> end
> 
> This is how I use it in my controller:
> 
> if @user.save
>     Notifier.registration_mail(
>         @params['new_user']['email'],
>         @params['new_user']['username'],
>         @params['new_user']['password'])
>         redirect_to :action => "registration_message"
> 
> The view (app/views/notifier/registration_message.rhtml, I've created it
> by hand ...) 

You don't actually call the registration_mail method directly. You
would call it as:

    Notifier.deliver_registration_mail(...)

This method is created automatically by ActionMailer. It's mentioned
here in the ActionMailer README:
http://am.rubyonrails.org/files/README.html (Directly above the
'Dependencies' header)

When called this it uses the template with the same name as the mailer
method automatically. You don't need to call 'redirect_to' (which
would redirect the users browser instead of sending a mail anyway).

Hope that helps.

-- Xian


More information about the Rails mailing list