ActiveMailer

Email needs to have somewhere to put the business logic surrounding it, and the controller is not the place for that.

See the disagreement:

Installation

  1. Add gem "active_mailer" to your Gemfile and run bundle install

  2. Run rails generate active_mailer:install

  3. Run rake db:migrate

Basic Usage

There's only a partial generator. In the mean time, making a new ActiveMailer class can be done like this.

  1. Run rails generate model FooEmail --no-migration --parent ActiveMailer::Base, you can pass any additional columns just like you would for a normal generate model.

  2. Run rails generate active_mailer:migration FooEmail

  3. Make the template for your email (in this case called foo_email.rb) in app/views/active_mailer/base/default_action_mailer/foo_email.html.erb

You're ready! You can send your email by making an instance of FooEmail, setting the appropriate details, and calling send!.

> f = FooEmail.new(:subject => "My Awesome Email", :sender => "[email protected]",
>                  :recipients => "[email protected]")
=> #<FooEmail id: nil, blahblahblah>

> f.send!
=> true

Advanced Usage

If your email is always going to have the same subject, sender, bcc, etc, then you can set those in the ActiveMailer object. Remember that it's really just an ActiveRecord object, so you can do anything in this class you can do in ActiveRecord.

Here's an example of using ActiveRecord associations to make sure there's a user for the email. It also includes setting the subject and sender by default.

class BeerEmail < ActiveMailer::Base
  belongs_to :user

  validates_presence_of :user

  def after_initialize
    self.subject = "It's Beer O'Clock"
    self.sender  = "[email protected]"
  end
end

Contributing

Setup the project with script/setup.

We use the Appraisal gem to test against multiple versions of Rails. By default rake will test every supported version of Rails, but you can isolate a specific vesion by using appraisal rails4.1 rake.

The tests use a micro Rails app that you should know about.

Bugs/Feature

Authors

  • Matt Gordon
  • Elijah Miller

Copyright (c) 2009-2015 Expected Behavior, LLC, released under the MIT license