Home » Ruby » How to Use Ruby on Rails for Local SMTP Email Testing

About Evan Glazer

Evan Glazer
Evan Glazer is a software engineer and self-starter at Edukate, where he uses Ember and Ruby on Rails and works with natural language processing and machine learning.

How to Use Ruby on Rails for Local SMTP Email Testing

When it comes to testing email templates for production; I think it is fair to say testing is key. Action Mailer is one of the most recognized frameworks for sending out emails. With Action Mailer you can send out emails through different providers easily, preview templates, and so forth. (Although from my work experience, I have always found it extremely difficult to have perfect emails the first time around.)

I have tried software tools like Litmus, BEE Plug-in, and others to help validate the email will be sent without any issues and display perfectly. I’ve found that using the Action Mailer preview method has never seemed to do the trick. I will teach you how to set up a local SMTP server so that all your email worries will be put to rest when it comes to rendering and sending out emails in your production environment.

Action Mailer

Action Mailer is a framework for designing email service layers. It’s a wrapper that goes around the action controller and mail gem, and provides a simplified way to make creating emails easily. The service layers can be used for sending out sign-up emails, invoices, notifications, and more.

Setting up the project

Let’s start by building the project:

rails new ror_smtp_testing

Let’s go into the folder cd ror_smtp_testing

Now lets go into the project and set up configs: If you’re using atom – lets say atom.

Mailcatcher documentation: Lets start with the Gemfile and add the following:

group :development, :test do
      gem 'mailcatcher'
    end
    gem 'actionview-encoded_mail_to'
    gem 'sidekiq'
    gem 'redis-rails'

Now bundle install to install the gems. Now let’s work on the configs setup.

Locate configs/environments/development.rb

We want to configure the setup by adding the following code into our file so that the Action Mailer and mailcatcher will be setup properly.

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.default_url_options = {:host => "localhost", :port => '3000', :protocol => "http"}
config.action_mailer.preview_path = "#{Rails.root}/tmp/mailers/previews"
config.cache_store = :redis_store, 'redis://localhost:6379/'

Make the sidekiq config

In this step, we will be setting up Redis and Sidekiq together so we can process the action mailer jobs.

config/initializer/sidekiq.rb

Sidekiq.configure_server do |config|
      config.redis = { :url => 'redis://localhost:6379/' }
  
    end 
  
    Sidekiq.configure_client do |config|
      config.redis = { :url => 'redis://localhost:6379/' }
    end 
  
    Sidekiq.default_worker_options = { throttle: { threshold: 4, period: 1.second } }

Now it is time to create a test mailer!

We need to build this test_mailer.rb in the mailers folder

class TestMailer < ActionMailer::Base
  default from: "Test <test@example.com>"
  layout 'mailer'
  def test_send(email)
    @email = email
    mail(to: @email, subject: 'Test Send')
  end
 end

Then build the view folder for this class which will be views/test_mailer/test_send.erb:

< %= @email %>

At this point we want to see that the the email output using mailcatcher.

Now lets start our server and start mailcatcher

rails s -p 3000

bundle exec sidekiq -q default -q mailers

How to install redis.

Start redis.

Time to test

Let’s go to the rails console

and

TestMailer.test_send('Test@test.com').deliver_now

Then we can check http://127.0.0.1:1080/ to see our test email and what it looks like.

Source Code

I have posted my source code for this project on GitHub so you can spin this server up and test it out!

Additional resources

Published on Web Code Geeks with permission by Evan Glazer, partner at our WCG program. See the original article here: How to Use Ruby on Rails for Local SMTP Email Testing

Opinions expressed by Web Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of