1 What is Action Mailer?
Action Mailer allows you to send emails from your application using mailer classes and views.
1.1 Mailers are Similar to Controllers
They inherit from ActionMailer::Base
and live in app/mailers
. Mailers also work
very similarly to controllers. Some examples of similarities are enumerated below.
Mailers have:
- Actions, and also, associated views that appear in
app/views
. - Instance variables that are accessible in views.
- The ability to utilise layouts and partials.
- The ability to access a params hash.
2 Sending Emails
This section will provide a step-by-step guide to creating a mailer and its views.
2.1 Walkthrough to Generating a Mailer
2.1.1 Create the Mailer
$ bin/rails generate mailer User
create app/mailers/user_mailer.rb
create app/mailers/application_mailer.rb
invoke erb
create app/views/user_mailer
create app/views/layouts/mailer.text.erb
create app/views/layouts/mailer.html.erb
invoke test_unit
create test/mailers/user_mailer_test.rb
create test/mailers/previews/user_mailer_preview.rb
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout 'mailer'
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
end
As you can see, you can generate mailers just like you use other generators with Rails.
If you didn't want to use a generator, you could create your own file inside of
app/mailers
, just make sure that it inherits from ActionMailer::Base
:
class MyMailer < ActionMailer::Base
end
2.1.2 Edit the Mailer
Mailers have methods called "actions" and they use views to structure their content. Where a controller generates content like HTML to send back to the client, a Mailer creates a message to be delivered via email.
app/mailers/user_mailer.rb
contains an empty mailer:
class UserMailer < ApplicationMailer
end
Let's add a method called welcome_email
, that will send an email to the user's
registered email address:
class UserMailer < ApplicationMailer
default from: '[email protected]'
def welcome_email
@user = params[:user]
@url = 'https://example.com/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
Here is a quick explanation of the items presented in the preceding method. For a full list of all available options, please have a look further down at the Complete List of Action Mailer user-settable attributes section.
- The
default
method sets default values for all emails sent from this mailer. In this case, we use it to set the:from
header value for all messages in this class. This can be overridden on a per-email basis. - The
mail
method creates the actual email message. We use it to specify the values of headers like:to
and:subject
per email.
2.1.3 Create a Mailer View
Create a file called welcome_email.html.erb
in app/views/user_mailer/
. This
will be the template used for the email, formatted in HTML:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Welcome to example.com, <%= @user.name %>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!