
Action Mailbox – Unboxing
There are many new shiny features are going to be unveiled in Rails 6. New methods for ActiveRecord, ActionText, Action Mailbox are some of them. We are going to talk about Action Mailbox today. What it is? How to use it? How it helped us to solve one real-time problem?
Introduction
Action Mailbox routes incoming emails to controller-like mailboxes for processing in Rails. It supports all major platforms or ingresses like Amazon SES, Mailgun, Mandrill, Postmark, and SendGrid. You can also handle inbound emails directly via the built-in Exim, Postfix, and Qmail ingresses.
Installation
As I mentioned before, Action Mailbox is going to be shipped with Rails 6 which is not released officially yet when I am writing this blog. You need to install the --pre
version of rails to use this feature.
Rails 6 needs Ruby version 2.5 or higher. But I met with class loading issue and after searching around found that there is an issue with version 2.5 and Action Mailbox. So use ruby 2.6 or higher version. Here is the issue link
Install rails 6 using below command. Make sure you are using ruby-2.6 or higher.
gem install rails --pre
I would strongly recommend creating gemsets for it. And create two files .ruby-version
and .ruby-gemset
in the root directory.
After installing the rails, create new rails project using below command. This will install all dependencies and take postgres as database
rails new action_mailbox -d postgresql
Now run bellow generator to create mailbox related migration and files.
rails action_mailbox:install
This will create migrations related to active storage and action mailbox tables. Run rails db:migrate
Run rails webpacker:install
to install asset files otherwise, it will throw an error while running rails server.
Start rails server using rails server
command and check localhost:3000
to check if everything is working fine.
You would need to configure your email service provider to post JSON data to your rails webhook when someone sends email to you.
You can find all the details on the official doc.
Use Case/Problem
One of our clients comes with a problem that, users send him lots of emails asking the price of the specific product and he would need to reply manually after checking the price.
Technically we would need to perform below actions:
- Read inbox with a subject contains SKU
- Read SKU from the body and identify SKU
- Call an external API to get product details
- Send a predefined email with product details to the sender
Read Also: Rails 6: Action Mailbox — Explained. Why?How? When?
Implementation
You might have already got the idea of how Action Mailbox will help us.
When you have ran rails action_mailbox:install
it would have created ApplicationMailbox.rb
In this file, you can define controller-like routes and based on that assign relevant mailbox. There is a specific syntax to define that routes which we will see later.
In our case, if the subject or message body has the word SKU then we would need to process the email using PriceFinderMailbox
and perform other actions.
Here are some rules for defining the routes based on the original doc.
We have used the proc like the route to identify if the subject or body has the specific word and accordingly choose the mailbox.
Behind the Scene
When your rails app got the request via webhook, it will hit the below controller which you can’t find in your code.
ActionMailbox::Ingresses::<email_ingress>::InboundEmailsController#create.
In our case <email_ingress>
is Postmark.
It will create an entry in below tables
ActionMailbox::InboundEmail
ActiveStorage::Blob
ActiveStorage::Attachment
ActionMailbox::InboundEmail
has status
field. This is how the status updates
During the processing of the inbound email, the status will be tracked. Before processing begins, the email will normally have the pending
status. Once processing begins, just before callbacks and the process
method is called, the status is changed to processing. If processing
is allowed to complete, the status is changed to delivered
. If a bounce is triggered, then bounced
. If an unhandled exception is bubbled up, then failed
.
It actually manages using enums
ActionMailbox::InboundEmail.statuses
{"pending"=>0, "processing"=>1, "delivered"=>2, "failed"=>3, "bounced"=>4}
This is how Action Mailbox added one for a feather to Rails framework and we solved the problem for the client.
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
We also specialize in Python, RPA, AI, Django, JavaScript and ReactJS.
Consulting is free – let us help you grow!
