#206 Action Mailer in Rails 3
Action Mailer has been rewritten for Rails 3 providing a much cleaner API with its use of the Mail gem.
- Download:
- source codeProject Files in Zip (97.3 KB)
- mp4Full Size H.264 Video (17.6 MB)
- m4vSmaller H.264 Video (12.1 MB)
- webmFull Size VP8 Video (33.4 MB)
- ogvFull Size Theora Video (23.9 MB)
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>