Rails 3 – Action Mailer

Today I will try to put down some basic changes that ActionMailer got with Rails 3.

Using a mailer model and a view, ActionMailer allows you to send emails from Rails application. Mailers get inherited from base class of ActionMailer i.e ActionMailer::Base and views reside with controller views.

Generating a mailer is simple.

$rails g mailer ReportMailer

In Rails 3, a default attributes can be easily specified:

class ReportMailer < ActionMailer::Base
 layout 'mylayout' # use mylayout.(html|text).erb as the layout
 default :from => "from@gauravsblog.com",
 :reply_to => "replyto@gauravsblog.com"
end

Adding a method like the one below will send an email on Report creation.

class ReportMailer < ActionMailer::Base
 default :from => "reporter@gauravsblog.com"
 def registration_email(report)
  @user = report.user
  @site_url = "http://gauravssite.com/login"
  mail(:to => report.user.email,  :subject => "Welcome to Gauravs Site")
 end
end

app/views/report_mailer should contain a corresponding view with name registration_email.html.erb which would act as a template.

The code for the email template is pretty trivial:

<html>
 <head>  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />  </head>
 <body>
  Welcome to Gauravs site
  Username for login: <%= @user.login %>
  Login here: <%= @site_url %>

  Thanks.
 </body>
</html>

Try to also keep the text format with the name registration_email.text.erb.

Lets say you created a report and you wanted to send an email to the user of the report, you could do something like this in the controller.

class ReportController < ApplicationController
 def create
  if @report.save
   ReportMailer.registration_email(@report).deliver
  end
 end
end

ActionMailer methods:
* headers – specify email header
* attachments – for attaching emails
* mail – sends the actual email

Set values in this way:

mail("X-Spam" => value)

Passing hash key value pairs to header

headers {"X-Spam" => value, "X-Special" => another_value}

Attachments can be easily added and the mime type gets automatically identified.

attachments['myfile.jpg'] = File.read('/path/to/myfile.jpg')

Inline attachment beocmes trivial:

attachments.inline['myimage.jpg'] = File.read('/path/to/myimage.jpg')

Use this in the email template

<%= image_tag attachments['myimage.jpg'].url %>

Here is the ActionMailer configuration that goes in config/environment.rb or config/env/desired_env.rb

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

Using SMTP (GMail)

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
 :address => "smtp.gmail.com",
 :port => 587,
 :domain => 'mydomain.com',
 :user_name => '<username>',
 :password => '<password>',
 :authentication => 'plain',
 :enable_starttls_auto => true
}

Thats all for now about mailer stuff ..

Advertisements

3 thoughts on “Rails 3 – Action Mailer

  1. If you are getting

    “hostname was not match with the server certificate” in your Rails 3 app, the crucial line is the :enable_starttls_auto => false.

    You might encounter this if you have been running on Ruby 1.8.6 for a while and decided to move to 1.8.7 at the same time you transition to Rails 3 from Rails 2.*

  2. Hi,

    I am trying to use Devise with Rails 3 and ruby 1.8.7.
    I’ve set the confirmable option so that users get a confirmation email when they register.
    However, I keep getting this error “undefined method ‘encode!’ for Confirmation:String” when I try to register a user.
    Can you please help me out with this?

  3. Thank you for another magnificent post. The place else could anybody get that kind of info in such an ideal way of writing? I have a presentation subsequent week, and I am at the search for such info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s