Send Mail

Rails makes it easy to send email. The ActionMailer gem is part of any
Rails installation.

Implementation of email closely follows the model-view-controller
architecture. To implement email, you’ll need:

    model
    view
    mailer

The “mailer” is similar to a controller, combining data attributes from
a model with a view file. Any methods we add to the mailer class can be
called from a controller, triggering delivery of an email message.

The model can be any we’ve already created. In this case, we’ll use the
Contact model, since it gives us access to the visitor’s name, email
address, and message.

We’ll create a mail-specific view file in the app/views/user_mailer/
folder. Our folder for mail-specific views will go in the app/views/
directory as a sibling of the app/views/layouts folder.

The Rails directory structure already gives us a folder app/mailers/ for
the mailer class and, not surprisingly, it is a sibling of the
app/controllers/ folder.

We don’t have to create the necessary folders and files manually, as the
rails generate command runs a utility to create what we need.
Mailing List

We’ll use the Rails model-view-controller architecture. We’ll need:

    Visitors model
    view for visitors#new
    Visitors controller with new and create methods
    routing for visitors#new and visitors#create

We’ll add a Visitor model that has a data attribute for an email
address. We already have a Visitors controller that renders the home
page using the file in the app/views/visitors/ folder. We’ll replace the
contents of the view file with a nice photo, a marketing message, and a
form.

Our Visitors controller new and create methods will be very similar to
what we created for the Contacts controller. Instead of saving data to
Google Drive, or connecting to Gmail to send a message, we’ll call a
method to save the visitor’s email address to a MailChimp mailing list