http://railscasts.com/episodes/206-action-mailer-in-rails-3
We will create a setup_mail.rb file under the config/initializers with this code (sending email from a gmail smtyp)
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "rails3mailer.com",
:user_name => "rails3mailer",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
We will then generate a mailer
rails g mailer registration
create app/mailers/registration.rb
invoke erb
create app/views/registration
invoke test_unit
create test/functional/registration_test.rb
class Registration< ActionMailer::Base
def registration_confirmation(user)
@username = user.name
mail(:to => user.email, :subject => "Registered", :from => "rails3mailer@gmail.com")
end
end
As you can see, it works like a Controller now. The @username is the instance variable storing the user.name from the user object that we are going to pass in when the method is called.
Next line, would be the mailing method. We specify that we will want to send the email to user.email (just like accessing an object instance variable), specifying the subject and from who.
This would be the view aka. what will the user receive.
Putting in whatever text will be the mail that the user would receive. And since we have passed the user name through the instance variable. We can actually use that to put the user name here like,
app/views/registration.text.erb
Hey, @username
You are onboard!
To make sure the mail is delivered, we will have to do one more thing.
In Ryan Bates' example, this can reside in the User controller after the User is created.
Registration.registration_confirmation(@user).deliver
We basically call that method and then Deliver!
0 comments:
Post a Comment