ActionMailer tutorial

What is ActionMailer?

It’s a framework for e-mail service, already integrated within rails!

What can it do?

  • Send an e-mail to new users
  • Send out forgotten passwords
  • Send invoices for billing
  • Anything that involves sending notifications to users via e-mail!

ActionMailers have:

mailer models- by creating methods inside the model, you can set certain variables which are used by the mail template (mailer views).

mailer views- these are .erb files with the same name as the method in your mailer model.

Getting Started

This tutorial will show how to create an e-mail notifier that automatically sends users links to new information posted in the database.

1. Generate a mailer model

script/generate mailer Notifier

This will create:

  • app/views/notifier
  • app/models/notifier.rb
  • test/fixtures/notifier
  • test/unit/postoffice_test.rb

2. Create a method for your mailer

You can create multiple e-mail models in this file. For this example we will create one called newgrants_notification.

These are the various configurations available for your method (from API)

  • recipients – Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the To: header.
  • subject – The subject of your email. Sets the Subject: header.
  • from – Who the email you are sending is from. Sets the From: header.
  • cc – Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the Cc: header.
  • bcc – Takes one or more email address. These addresses will receive a blind carbon copy of your email. Sets the Bcc header.
  • sent_on – The date on which the message was sent. If not set, the header wil be set by the delivery agent.
  • content_type – Specify the content type of the message. Defaults to text/plain.
  • headers – Specify additional headers to be set for the message, e.g. headers ‘X-Mail-Count’ => 107370.

codetitle. app/models/notifier.rb

class Notifier < ActionMailer::Base
  def newgrants_notification(user, grants)
     recipients user.email
     from       "admin@grantitude.org"
     subject    "New grants added to grantitude.org"
     body       (:user => user, :grants => grants, :url_base => 'http://locathost:3000/grants/show')
  end
end

In the body, we are creating variables that we can use in our template. In this example, we are specifying user, grants, and :url_base since this is all information that we are displaying in the e-mail.

3. Create an e-mail template

We need to create a mailer view that corresponds to the method that we made above. In this case, we need to create newgrants_notification.rhtml. It should be put in app/views/notifier/.

codetitle. app/views/notifier/newgrants_notification.erb

hello <%= @user.login %>,

These new grants have been added to the database:

  <% @grants.each do |grant| %>
    <%= "#{@url_base}/#{grant.id}"  %>
    <%= grant.title %>
  <% end %>

The loop goes through each grant and puts a link to its web page.

4. Create a RunnerScript for automatically sending notifications
This allows you to run a command in the terminal that will send out the e-mails to the users designated.

Create a new mother folder called “bin”, creating “notify_daily” or whatever name you would like.

codetitle. bin/notify_daily.rb

@users = User.find :all, :conditions => ['wants_notification = ?', true]
@grants = Grant.find :all, :conditions => ['created_at > ?', 1.week.ago]

@users.each do |user|
   Notifier.deliver_newgrants_notification (user, @grants)
   puts 'sent to %s' % user.email

The following piece uses a column in users that designates whether or not users want an e-mail:

 
@users = User.find :all, :conditions => ['wants_notification = ?', true]

The following piece designates the conditions for the grants we want to use in the e-mail, in this case want grants created in the last week:

@grants = Grant.find :all, :conditions => ['created_at > ?', 1.week.ago]

5. Sending the e-mail notification

Running the following in your terminal calls on notify_daily.rb. It will put the e-mail addresses that it sent the message to.

script/runner bin/notify_daily.rb

Links