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
- Set up Ruby on Rails projects with CodeShip Basic
- Test Ruby on Rails apps with CodeShip Basic
- Hear the webinar on why CI/CD needs feature flags
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. |