HowTo: Add email support
October 10, 2008 Leave a comment
Download the gem from this site and install using
gem install actionmailer-2.1.0.gem
After installing, configure the mailer. In config/environment.rb, add the following lines after the Initializer block,
ActionMailer::Base.delivery_method = :smtpActionMailer::Base.smtp_settings = {
:address => ‘>address<’
:port => ‘, # default: 25
:domain => “”
:user_name => ”,
:password => ”,
:authentication => :login # :plain, :login or :cram_md5
}
Also, change the config.action_mailer.raise_delivery_errors option in config/environments/development.rb to true.
Create a mailer for the project.
ruby script/generate mailer Emailer
Let’s start by adding the following lines to the model.
def signed_up(recipient)
recipients recipient
subject "[Signed up] Welcome #{recipient}"
from "xxx@abc.de"
body :recipient => recipient
end
This method will be used to send the email.
IMPT: the “from” line should have a valid email address on the server or the mail won’t be delivered.
The body of the email is created by creating a view of the same name. So in views/emailer/signed_up.html.erb, place the body of the email you want to send, for example:
Hello there,Mr. <%= @recipient %>
To send the email, you need to append “deliver_” to the method name. So if we need to send the mail to xxx@abc.de, use the following lines
Emailer.deliver_signed_up("xxx@abc.de")