#61 Sending Email
Jul 23, 2007 | 7 minutes | Mailing
This is a brief guide to sending email in Rails. See how to configure the environment, generate a mailer, create a template, and deliver the mail.
Resources
config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp
# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
:address => 'smtp.example.com',
:port => 25,
:domain => 'www.example.com',
:authentication => :login,
:user_name => 'www',
:password => 'secret'
}
config.action_mailer.raise_delivery_errors = true # set delivery method to :smtp, :sendmail or :test config.action_mailer.delivery_method = :smtp # these options are only needed if you choose smtp delivery config.action_mailer.smtp_settings = { :address => 'smtp.example.com', :port => 25, :domain => 'www.example.com', :authentication => :login, :user_name => 'www', :password => 'secret' }
users_controller.rb
UserMailer.deliver_registration_confirmation(@user)
UserMailer.deliver_registration_confirmation(@user)
user_mailer.rb
def registration_confirmation(user)
recipients user.email
from "webmaster@example.com"
subject "Thank you for Registering"
body :user => user
end
def registration_confirmation(user) recipients user.email from "webmaster@example.com" subject "Thank you for Registering" body :user => user end
registration_confirmation.rhtml
Hi <%= @user.name %>, ...
Hi <%= @user.name %>, ...
