How to debug Mailgun email handling in local Rails development environment

I’m working on a Rails application that’s deployed on Heroku and uses Mailgun for handling incoming email messages. Right now I’m having trouble testing my email processing code locally. Every time I need to make changes, I have to deploy to Heroku first which is really slow and annoying.

Is there a way to test Mailgun functionality on my local development server? What’s the best approach to set this up so I can debug without constantly pushing to production?

Any help would be appreciated!

just use the mailcatcher gem for local testing - way easier than setting up ngrok. it catches all outgoing emails in a web interface so u can see what’s being sent without hitting Mailgun servers. throw in some Rails console testing to simulate inbound webhooks and ur set. saves a ton of deployment time.

Use ngrok to debug Mailgun emails locally. It creates a public URL that tunnels to your local Rails server, allowing Mailgun to send webhooks directly to your development environment. Simply install ngrok, run it on the port your Rails app uses, and point your Mailgun webhook to that URL. It’s advisable to set up a separate Mailgun domain for development to avoid interfering with production. Additionally, implement some logging in your email processor to track the incoming data from Mailgun. The free ngrok version suffices; although the URLs change upon restart, this is manageable for debugging purposes.

Hit this same problem six months back. I set up a local webhook endpoint with ngrok and it worked great. Created a dedicated Rails controller action just for dev that mimics Mailgun’s webhook format. You can manually fire off emails by crafting POST requests with the same params Mailgun sends. This way you can test email processing without internet or dealing with ngrok’s constantly changing URLs. Also try the mailgun-ruby gem’s test mode - it intercepts API calls during development. Just make sure you’ve got solid test data that matches real Mailgun payloads. Grab some examples from your production logs first.