Building Mailer Functionality
Learn how to user ActionMailer in Rails and build the initial notification email functionality.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that our core functionality is almost done, we are going to start building our email functionality.
Go to command line and type

rails g mailer ManagerMailer

This command generates some files for us.

large

If you see, rails has generated a few rspec files for us. I'm not going to be using them since sending an email is not the core functionality of our system.

Also, you can find a directory called mailers and two files in it called application_mailer.rb and manager_mailer.rb. Open application_mailer.rb and here, you'll find a callback called default from:. Edit this value to an email address that comes from a domain you own. I'm not sure if I have access to this client's domain name, so for now, I'll just say "mailer@devcamp.com".

# app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "mailer@devcamp.com"
  layout 'mailer'
end

Now, go to manager_mailer.rb, and create a method called email. In this method, we are using a method called mail to send email to the manager.

# app/mailers/manager_mailer.rb

class ManagerMailer < ApplicationMailer
  def email manager
    @manager = manager
    mail(to: @manager.email, subject: 'Daily Overtime Request Email')
  end
end

Next, you can decide how you want your email to look. You can have a HTML-based coding, but for this application, I'm simply going to have plain text.
If you scroll down a little bit on your directory structure, you'll find a folder called manager_mailer. Inside this folder, create a text file called email.text.erb. One thing you'll have to note is that the name of the file should be the same as the name of the method you use in manager_mailer.rb. So, if you've named your method as welcome_email, then your file name should be welcome_email.text.erb. Otherwise, it won't work.

In the text file, we can put pretty much anything we want. I'm going to keep it simple though. We have access to the instance variable manager, so we'll use it here.

<%# app/views/manager_mailer/email.text.erb %>

Hi <%= @manager.full_name %>,

There are new overtime requests for you to review. Please click this link to access them.

Link: <%= root_url %>

From the mailer robot.

Next, we have to add some config settings. Go to development.rb and add this code. You can also find these settings on the mailer documentation page at "www.rubyonrails.org"

# config/environments/development.rb

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

That's about it. Let's check it on the console. You can call the email method like this:

ManagerMailer.email(AdminUser.last).deliver_later

And this should give you this message:

large

You may get an error if the mail server in your domain is not running, so don't worry about that. Just make sure you can see the message you have in the text file.

Resources