Monday, March 17, 2008

Rails send emails with actionmailer and gmail

The first step is to configure your rails application to work with gmail, I just copied the 2 simple steps from the Preston Lee's article:

1. Save this code as lib/smtp_tls.rb within your rails app;
2. Add this code to config/environment.rb

The second step is to learn to use actionmailer, just follow the document "How to send emails with action mailer" to do it then you will understand how it works pretty fast.

1. ruby script/generate mailer Notifier signup order_created
2. add code to your app/models/notifier.rb, such as

def signup( user )
# Email header info MUST be added here
recipients user.email
from "accounts@mywebsite.com"
subject ¡°Thank you for registering with our website¡±
# Email body substitutions go here
body :user=> user
end

3. add code to /app/views/notifier/signup.erb
Dear <%= @user.first_name %> <%= @user.last_name %>,
Thanks for signing up with My Website!

4. use the mailer in where you want.
Notifier.deliver_signup(user)

1 comment:

Anonymous said...

This didn't work for me, until I changed the authentication from 'plain' to 'login;