There are cases when application receives large number of inbound emails. And if we have to process those emails, then one has to go through each email in order to perform operations.
Let’s consider a use case of HR domain. An organization has sent an email asking candidates to submit resumes for an opening. All candidates are replying to this email with their resumes as attachments. Now, we need to upload these resumes to cloud and create a database entry for each resume.
Steps to achieve this:
- We’ll go through each mail.
- We’ll download the resume.
- Now, we’ll upload the resume to cloud and create entry in the resume table.
It looks like a tedious task.
Introducing Action Mailbox
Rails 6 has introduced Action Mailbox for processing inbound emails. It routes incoming emails to controller-like mailboxes for processing in Rails. It supports all major platforms like Mailgun, Mandrill, Postmark and SendGrid.
Installation and Implementation
Let’s create a new application
feedback_collector by running the following command.
action_mailbox by running
This command will also install
Active Storage, which stores the emails which come in.
It saves these emails and keep track of it whether it has been processed or not.
Then it loads an
Active Job to process it and delete that email once it is done.
Even after deletion, it keeps track of the id and checksum to avoid
the processing of the same email if it comes again.
Above command generates two migrations for
And it also creates
Now let’s generate scaffolds for
Run the migrations:
action_mailbox table’s schema contains columns like
status can be
message_checksum are there to avoid duplication.
ApplicationMailbox class looks like this.
Here we can define routes for emails. For ex:
We have specified one route for redirecting all the emails to
We can also specify routes in regex format.
This expression can match emails like
Now let’s create
FeedbacksMailbox class contains a method named
process, to process the emails.
In this class, we can access
If we have to perform some operations before processing the email,
we can do that with
Now, we have got the
But to save the feedback for a product, we’ll need the
To get the
product_id, we can specify the reply email’s
regex in such a way that it contains the
If reply email is
then using above regex we can get
Now, Let’s try to process the email and save the user’s feedback.
As we have access to
Testing on Development Environment
To test this on development enviroment,
we can simply go to
and deliver an inbound email.
Based on the
to email, it’ll route to mailbox and process the email.
Configuration for Production
Action Mailbox for the prodcution environment,
we need to specify the
We also need to generate a strong password that
Action Mailbox can use to authenticate requests to the
We have to store password in the encrypted credentials as
Instead of storing in credentials, we can also provide this password in the
RAILS_INBOUND_EMAIL_PASSWORD environment variable.
Now, we need to configure inbound webhook to forward inbound emails to
/rails/action_mailbox/postmark/inbound_emails with the username
and the password we previously generated.
Following will be our webhook URL:
For example, we’ll use following services.
- Sendgrid (Mailing Service)
- Freenom (Domain Registration Service)
- ngrok (Provides Public URL for exposing local web server)
All these services are free.
Let’s create accounts on SendGrid, Freenom and ngrok.
ngrok installation, please refer to steps mentioned in this guide.
Now, let’s register a free domain by using this link.
Enter any domain like
actionmailbox in the search box and hit
Check Availability button.
It’ll give all the free options and select any option.
Now, you can view your domain in the
My Domains section under
Services nav item.
After this, we need to authenticate our domain on
For authentication, follow these steps:
- Click here
Other Host (Not Listed)and type
- Enter domain name in the
- It’ll show three
CNAMErecords that we need to add in the domain management section of
- Go to
My Domainssection on
Freenomin a new tab
Manage Freenom DNStab
- Now, we need to add those three CNAME records here.
- Also create a
MXrecord. We can keep
- Now go back to
SendGridtab, check the checkbox and click
- If it fails then wait for 15-20 minutes and again click
- Visit this link and
it’ll show status as
Verifiedfor our domain.
Next step is to add following SMTP configuration in
Start the server by running the command
After that, run
./ngrok http 3000 in a new tab.
Forwarding value above, our server public url is
Make a note of your URL for the below example.
Now, we’ll follow the steps mentioned in the
Conguration for Production section.
- Create an ingress password and add in the credentials.
- Go to Inbound Parse
section of sendgrid and click
Add Host & URL.
Select the domain, set destination url as
https://actionmailbox:INGRESS_PASSWORD@SERVER_PUBLIC_URL/rails/action_mailbox/postmark/inbound_emailsand check the option for
POST the raw, full MIME message.
Let’s create a mailer for asking the feedback of the product by running the following command:
Add the following code related to sending mail in
In the above example,
REPLY_TO_MAIL_ADDRESS should be
PRODUCT_ID should be the product for which we are asking the feedback.
We can trigger the feedback email by running the following command.
By running the above command, user will receive an email.
We have already setup the routes for
So if user replies to this email, then it’ll call the
process method of
and will process the email.
We looked at the basics of Action Mailbox, how to install, implement and configure it. And also provided the setup for example similar to Production.