Prev | Current Page 425 | Next

Brad Ediger

"Advanced Rails"

First, load the framework,
either using RubyGems or a newer Subversion checkout:
# gem version
require 'rubygems'
require 'action_mailer'
# or edge version
require 'vendor/actionmailer/lib/action_mailer'
Next, set the outgoing mail server settings. All settings are optional.
# Default is :smtp; also accepts :sendmail or :test
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => 'localhost',
:port => 25,
:domain => 'example.com', # HELO example.com
:authentication => :cram_md5,
:user_name => 'me',
:password => 'secret'
}
ActionMailer needs a template directory in which to look for email templates:
ActionMailer::Base.template_root = 'views'
288 | Chapter 9: Incorporating and Extending Rails
Mailer classes and their email templates are defined just as they are in Rails:
class Mailer < ActionMailer::Base
def quota_exceeded_notification(user)
from "System Administrator "
recipients name_and_email(user)
subject "Your account is over the quota"
body {:user => user}
end
private
# "John Smith "
def name_and_email(user)
"#{user.full_name} <#{user.email}>"
end
end
The template follows the usual pattern, and is located under our template root, in
views/mailer/quota_exceeded_notification.erb:
Dear <%= @user.name %>,
Your account is over its storage quota. You are currently using
<%= human_size(user.


Pages:
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437