Email needs to have somewhere to put the business logic surrounding it, and the controller is not the place for that.
See the disagreement:
gem "active_mailer"to your
rails generate active_mailer:install
There's only a partial generator. In the mean time, making a new ActiveMailer class can be done like this.
rails generate model FooEmail --no-migration --parent ActiveMailer::Base, you can pass any additional columns just like you would for a normal
rails generate active_mailer:migration FooEmail
Make the template for your email (in this case called
You're ready! You can send your email by making an instance of
FooEmail, setting the appropriate details, and calling
> f = FooEmail.new(:subject => "My Awesome Email", :sender => "email@example.com", > :recipients => "firstname.lastname@example.org") => #<FooEmail id: nil, blahblahblah> > f.send! => true
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@example.com" end end
Setup the project with
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.
- Matt Gordon
- Elijah Miller
Copyright (c) 2009-2015 Expected Behavior, LLC, released under the MIT license