| (TUTORIAL) - How to Create and Manage Your Mailers |
Key Notes: Our pretend rails app is going to be called (mailbag). If I
reference our rails application at any point in this tutorial, I will
use mailbag as a descriptive reference. I’m going to assume you know
how to create a starting rails application. I’m also going to assume
you understand the MVC structure (Model/View/Controller) and how to find
environment.rb, routes.rb, and that you also have the ability to
navigate through your rails application.
For this tutorial, it would be best that you go ahead and create a
mailbag rails application and follow along with your test app. I’m not
going to go into detail on how to setup sweepers in this tutorial.
However, you’ll be able to understand why they have their own folder
structure once we’re finished with the tutorial.
If you are using Ruby 1.8.6 or lower you will also have to download a
If you are using Ruby >= 1.8.7 or Ruby >= 1.9 you DO NOT need the
– Create your Rails app for mailbag
– You can use any database (I use mysql)
– Run your rake db:create to create your database
– Keep your rails app open in your IDE/editor environment
– Follow the tutorial below and have some fun
I see a lot of people asking about mailers and I remember when thinking
about mailers felt a lot like ruminating on Sunday night in preparation
for a Monday morning work day. Well, hopefully when this tutorial is
finished, you will no longer feel this way.
I’m going to break down the tutorial into multiple pieces for better
clarity and readability.
What you will accomplish in this tutorial
When you are finished, you’ll be able to use a Google Mail account
(gmail account) to process all mail for your site, have a solid
understanding of observers and how to use them, and will be able to send
both text or html emails to any of your site visitors. In addition, your
mailers will be better organized.
Organizing your Mailers into an easy-to-read structure
Before we start with mailers, wouldn’t it be nicer to have them in an
organized structure? If we didn’t organize them, they would be
scattered across many different folder paths. With rails and a few
lines of code, you can organize all of your mailers, observers, and
sweepers into their own unique folder structure.
Open your Config --> Environment.rb file.
Place in the following code:
Create your physical folder structure. Inside of your mailbag -->
app directory, create sub-directories for mailers, observers, and
- Create a mailers view directory to hold all of our mailer views.
This views sub-directory will be located in mailbag --> app --> mailers.
- Create an application_mailer.rb file that will be located in mailbag
–> app --> mailers.
Final Directory Structure
The final directory structure is also shown in a picture that you can
view to compare notes with:
So, what have we done here? Well, first we let our rails environment
know through the environment.rb file that we will be loading paths to
our new directories. We also created an ApplicationMailer class that is
part of ActionMailer::Base and declared where our template views would
be located within that file. By doing so, we now control where all of
our mailers will be located. And, this means we can now hold all of our
mailers in one central location.
By itself, it does nothing at this point. All we’ve done is organized
where our mailers will operate from.