Using Action Mailer: The Basics

Action Mailer is a wrapper for Action Controller and the Mail gem. It’s used to send emails from your app. It works very well for automatated emails like welcome emails and confirmation emails.

There is some set up before actually being able to send an email. In the config/environments/appropriateenvironmentfile.rb file you’ll need to give the correct specifications for the Action Mailer configuration.

1
2
3
4
5
6
7
8
9
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'baci.lindsaar.net',
  :user_name            => '<username>',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

The example above uses smtp as the delivery method, but other options include sendmail, test, and file. The latter two are generally used during development. Test saves an array of emails sent and is useful during functional and unit testing while file causes Action Mailer to write the email to a file in the tmp/mails directory. SMTP (Simple Mail Transfer Protocol) is the protocol for sending email while sendmail is one of the applications that can send mail through SMTP standards as well as other mail transfer protocols. Choosing SMTP means you have to supply an SMTP server (visible above) while with sendmail it is your local machine that is acting as the server.

After configuring Action Mailer you should be able to send an email, but before you do, you’ll need a mailer. You generate a mailer in the same way you would generate any other rails resource.

1
rails generate mailer UserMailer

The mailer, views, and tests will be generated for you. The information for the email you want to send goes in the mailer. So we can define a confirmation email in our mailer below.

1
2
3
4
5
6
7
8
9
class UserMailer < ActionMailer::Base
  default :from => "info@example.com"

  def confirmation_email(user)
    @user = user
    @url  = "http://example.com"
    mail(:to => user.email, :subject => "Thanks for signing up!")
  end
end

To have something to render in the email, the views need to be set up as well. You should create both an email view in erb/html and text. They will go in app/view/invitation_mailer and the text file should be named invitation_email.text.erb while the html file is named invitation_email.html.erb.

Assuming you already have a Users controller, now it’s time to tell it when to send the email. Since in this case, we’re sending an email to confirm their signing up at our site, we want the email sent when the user is created. This occurs when the user is saved during create in the Users controller. In the create method we can tell it to send the email if the user is saved like so:

1
2
3
4
5
def create
    @user = User.new(params[:user])

if @user.save
        UserMailer.confirmation_email(@user).deliver

At this point you are now able to send an email from your app. There are tons of ways to extend the functionality of Action Mailer though and we’ll get into that in later posts.