Rob Zolkos

January 5, 2024

Debugging production ActionMailbox issues in development

When receiving and processing incoming emails with ActionMailbox, you may come across issues with particular emails that you need to debug.   There is a tiny section in the Rails Guides about how to work with ActionMailbox in development.  But how do we get an email that caused issues in production and debug it in development with the tools Rails provides?

To tackle this, the first step involves grabbing the raw email text from production.  This is saved in the database for 30 days by default so you should have access to it.   Open up your trusty rails production console: 

inbound_email = ActionMailbox::InboundEmail.find(123)

Replace the 123 id with something relevant to your case.  It may even be .last.   You then need to output the raw email text

puts inbound_email.mail

Copy the email to your clipboard - you will need it for the next step.

Start up your Rails app in development and visit http://localhost:3000/rails/conductor/action_mailbox/inbound_emails

You'll see a screen like this:   Click on the New inbound email by source link. 
conductor.png


Here you will be able to paste in the raw email source you copied from production:

CleanShot 2024-01-04 at 22.45.50@2x.png


Press the "Deliver inbound email" button and that will route that same email that was causing issues in production into your development system so you can debug it.  From there you can halt execution at various points of the processing code and fix the issue [that is outside the scope of this post, but here are some handy guides].

Once you have found and fixed the issue, deployed the fix to production, all will be well.  Not so fast.  The fix will work for all future incoming mails.  But what about the ones that didn't work.  The ones that failed before the fix?

They will need to be reprocessed.   To do that, you need to know the id of the ones that didn't work, and apply the reroute mechanism to process them again.  

In the production console:

inbound_email = ActionMailbox::InboundEmail.find(id)
inbound_email.pending!
inbound_email.route_later

This will reprocess the incoming email with your fixes applied.


About Rob Zolkos

Ruby on Rails developer here to help solo devs and small teams amplify their impact. Embracing the Rails way with Hotwire and Stimulus. Email: robzolkos@hey.com Twitter: https://twitter.com/robzolkos