Setting Up ActionMailbox in Ruby on Rails for Inbound Email Processing In this tutorial, we walk through the installation and setup of ActionMailbox for processing inbound emails in a Rails application. This guide covers creating a new Rails application, adding ActionMailbox using the generator command, and understanding dependencies like ActiveStorage. We also discuss the setup of the Inbound emails table and the application mailbox for routing emails. Furthermore, we create a simple 'support' mailbox and use the Rails Conductor to simulate and test inbound email processing. Debugging and additional features like reprocessing and incinerating emails are also demonstrated.

00:00 Introduction to ActionMailbox

00:15 Setting Up a New Rails Application

00:40 Installing Action Mailbox

01:06 Understanding Action Mailbox Components

02:49 Creating and Configuring Mailboxes

04:34 Testing with Rails Conductor

07:05 Debugging and Reprocessing Emails 08:28 Conclusion and Recap

Transcript

[00:00:00] Welcome back. In this video, I'll be covering how we can install and set up ActionMailbox to begin processing inbound emails for our Rails application. I'll be covering everything that we need to be able to get started and begin processing inbound emails to our application. For this video, I'll be creating a brand new Rails application to install ActionMailbox into, but the process will be pretty similar if you're looking to add ActionMailbox into an application that you already have.

For creating the new application, I'm just going to run our Rails new command to get started with a simple sample application

with my new Rails application. I can add action mailbox with a single generator command, which is easy as running in Rails action mailbox install

a couple important things to note about everything that was created here. One [00:01:00] is you may notice that we're also. adding active storage along with Action Mailbox. The reason for the active storage dependency is because Rails will store the inbound email as a active storage attachment while it's waiting to be processed and sent to the correct mailbox.

The migrations created by the active storage dependency are your standard active storage tables, so we don't really need to go too much into them, but I did want to briefly go over the Inbound email table that's being created here and added from Action Mailbox. This is going to create the Action Mailbox inbound emails table which is going to be responsible for storing our inbound email.

Until rails is ready to process it and route it to the correct mailbox. The last file that was created is our application mailbox for action mailbox, the application mailbox is going to act a lot like your routes file would in your normal rails app. The application mailbox will receive the inbound email and then direct it to the correct mailbox based on some different [00:02:00] matching criteria.

We could see with our generated application mailbox, we have a nice example here of what it's looking for. So there are a couple of different options, but the main way for routing your inbound emails is by giving it a match statement that's going to map to the to address from the inbound email.

So the destination for the inbound email on then in this example, somewhere would be the name of the mailbox that we're routing it to. And now with everything set up, all we need to do is run our database migrations to create the new tables added by action mailbox

up to this point, all that we've really done is install and configure action mailbox. We don't have any mailboxes ready to process any of the inbound emails. I'm just going to create a simple example one here. I'm going to call it support. So we have a handy generator command. We just need to pass it rails generate mailbox and then the name of the [00:03:00] mailbox that we would like.

So for this example, I'm going to use support. Okay, and we see a support mailbox has been created as well as the test. So let's take a look at that now. And here we have our support mailbox that was created from running the generator. Looking into everything. It's pretty simple. We have a single method called process, which is going to be called whenever our inbound email is picked up to be processed here in our application mailbox.

We already have one routing statement here and commented out for us that we can use it as an example to get started. So we would be able to do something. Let's just keep it simple

like this. So if I was to send an inbound email with something that would match Okay. Support in the two address that I'm sending the inbound email to is going to get directed to the support mailbox that we just created. This is how you're going to be [00:04:00] using this. Once you begin to add multiple mailboxes, but.

Whenever just getting started, there is a handy all method that we can pass to our routing statement to make sure that any inbound email will be routed to this specified mailbox. I'm just going to remove this year and we can do routing and we'll say all and that's going to route to our support mailbox.

So now, anytime I get any, Inbound email to the application. Our application mailbox will route that directly to our support mailbox. You may be wondering how we're going to send inbound emails to our local application to start testing things out and making sure everything is working correctly. So this is where the Rails Conductor comes in.

This is a URL that's available to us whenever our Rails application is running that lets us fill out a simple form to simulate sending an inbound email to our application. To view the Rails Conductor with your Rails application running, we [00:05:00] can navigate to rails slash conductor slash action mailbox slash inbound emails.

So navigating here, we have two options. The first one is new inbound email by form. This is a simple form that will allow us to fill out the information and simulate sending an inbound email to our Rails application. We also have the new inbound email by source option. So for this option we would upload the raw source of an email that will then be converted and sent to our inbound email processing.

This is a great way that you can simulate or reproduce issues that you may be seeing on production or maybe some specific mail clients or email service providers. Creating a new inbound email in the Rails Conductor by source is a great option for production issues or reproducing things, but for the most part, I do most of my development here in the just plain Rails Conductor form section.

So now with our support inbox, we can fill [00:06:00] out our information and send an inbound email to our mailbox.

After submitting our inbound email, we land on the detail page for the inbound email object. We can refresh here and we can see that it was delivered. We can also click here to see the full email source. Now with our inbound email delivered from the Rails Conductor if we navigate back to the first page that we landed on We can see that we have a list of the inbound emails that if we click on it, will give us some more detail and also allow us to either route it again, which will send it back to wherever it was originally delivered to or incinerate it.

This incinerate option this is a handy thing that Action Mailbox includes where it will automatically incinerate or delete the active storage object. That was created to [00:07:00] store your inbound email prior to it being processed automatically after 30 days. Let's take a look at reprocessing this email.

Inside our support mailbox within the process method I'm going to add this debugger statement. This is an alias for the Ruby debug gem that will open a debugger wherever I'm running my Rails server whenever the inbound email is beginning to be processed. So we have this set up can navigate back.

This was already delivered to the support mailbox. So now I could just run route again, and we should hit our debugger in our server. Okay. So I don't see anything happening. So it looks like the page is hanging. And if I navigate here, we can see my debugger is open and I'm within the context of the support mailbox.

This is a great way to get some more context around what exactly is happening while your inbound email is being processed. So if we just call self here we could see the return object is our ActionMailbox [00:08:00] inbound email. This maps to that table that we saw previously when we installed and created everything.

We have our status, message ID, checksum, created at. And because there's no processing logic here within the process method, there's not really anything else to do with this point. I wanted to show this example as an easy way to send inbound emails with the Rails conductor, as well as some ways that you can inspect and debug some of the inbound emails that you'll be receiving and working with.

So for this video, we've covered how we can install, configure, and prepare ActionMailbox to begin receiving inbound emails to process. We've also taken a look at how we can generate a simple mailbox, as well as ways that we can simulate sending an inbound email to our Rails application.