RailsCasts - Ruby on Rails Screencasts

RailsCasts Pro episodes are now free!

Learn more or hide this

Action Mailer in Rails 3

#206 Action Mailer in Rails 3

Mar 22, 2010 | 12 minutes | Rails 3.0, Mailing
Action Mailer has been rewritten for Rails 3 providing a much cleaner API with its use of the Mail gem.
Click to Play Video ▶
Tweet
  • Download:
  • source code Project Files in Zip (97.3 KB)
  • mp4 Full Size H.264 Video (17.6 MB)
  • m4v Smaller H.264 Video (12.1 MB)
  • webm Full Size VP8 Video (33.4 MB)
  • ogv Full Size Theora Video (23.9 MB)
Browse_code Browse Source Code

Resources

bash
rails mailit
cd mailit
rails g scaffold user name:string email:string
rake db:migrate
rails g mailer user_mailer
bundle install
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
 :address => "smtp.gmail.com",
 :port => 587,
 :domain => "railscasts.com",
 :user_name => "railscasts",
 :password => "secret",
 :authentication => "plain",
 :enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
 default :from => "ryan@railscasts.com"
 
 def registration_confirmation(user)
 @user = user
 attachments["rails.png"] = File.read("#{Rails.root}/public/images/rails.png")
 mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered")
 end
end
users_controller.rb
UserMailer.registration_confirmation(@user).deliver
Gemfile
gem "mail", "2.1.3"
lib/development_mail_interceptor.rb
class DevelopmentMailInterceptor
 def self.delivering_email(message)
 message.subject = "#{message.to}#{message.subject}"
 message.to = "ryan@railscasts.com"
 end
end
views/user_mailer/registration_confirmation.text.erb
<%= @user.name %>,
Thank you for registering!
Edit profile: <%= edit_user_url(@user) %>
views/user_mailer/registration_confirmation.html.erb
<p><%= @user.name %>,</p>
<p>Thank you for registering!</p>
<p><%= link_to "Edit profile", edit_user_url(@user) %></p>
loading

AltStyle によって変換されたページ (->オリジナル) /