Mailcacher In Ruby on Rails

BharteeTechRubyOnRails
1 min readApr 8, 2023

--

To create a new Rails web application for the mail caching feature, you can start by running the following commands in your terminal:

rails new mail_cacher_web_app
cd mail_cacher_web_app

If you are like me trying to install mailcatcher gems on Linux and getting errors due to outdated event machine gem dependency, see below.

gem install mailcatcher -- --with-cflags="-Wno-error=implicit-function-declaration"

The author recommends keeping the gem out of the Gemfile. We need the flags to compile dependencies.

Next, you can configure your development environment by adding the following code to the development.rb file:

config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: '127.0.0.1', port: 1025, domain: '127.0.0.1' }

To start Mailcatcher, run mailcatcher in your terminal.

To generate a mailer, you can run the following command:

rails g mailer order_mailer

This will generate a mailer file order_mailer.rb in the app/mailers directory of your Rails application.

Inside the generated mailer file, you can define a method that represents the email action you want to send. For example:

# app/mailers/order_mailer.rb
class OrderMailer < ActionMailer::Base
default from: 'noreply@example.com'

def confirmation_email(order)
@order = order
mail(to: @order.email, subject: "Your Order #{@order.id})
end
end

You can customize the email action according to your needs.

--

--

BharteeTechRubyOnRails
BharteeTechRubyOnRails

Written by BharteeTechRubyOnRails

Ruby on Rails Developer || React Js || Rspec || Node Js

No responses yet