I built a MERN stack application that handles user registration and email verification using Mailgun. Everything worked perfectly during local development, but I’m running into issues after deploying to Heroku.
When I try to open my deployed app with heroku open, I get an “Application Error” message. Running heroku logs --tail shows that the Mailgun API key isn’t being recognized, even though I’ve added it to the Heroku config variables.
I installed the Mailgun addon on Heroku, but the documentation only covers Ruby implementations. How can I properly configure Mailgun to work with my Node.js backend on Heroku? Are there any specific steps or alternative approaches I should consider for this setup?
I encountered a similar problem with my Node.js application on Heroku when integrating Mailgun. The primary culprit often relates to environment variable loading. Ensure that you’re retrieving the API key using process.env.MAILGUN_API_KEY, avoiding any hardcoded values. Additionally, check your Mailgun account for domain verification, as operating in sandbox mode can lead to discrepancies. When deploying, the initialization of the Mailgun client is crucial—sometimes the format of the domain can differ from your local setup. To troubleshoot, logging a portion of your API key may help confirm that the environment variables are being utilized correctly. Also, remember to execute heroku restart after setting up those configuration variables.
check if ur using the right mailgun domain in production vs development. the heroku addon might’ve provisioned a different subdomain than ur local setup. also make sure ur heroku dyno isn’t sleeping - free dynos go to sleep and cause weird api timeouts. add some console logs to see exactly where ur email sending code is failing.
Mailgun’s addon creates environment variables with different names than you’d expect. Look for MAILGUN_SMTP_LOGIN or MAILGUN_SMTP_PASSWORD instead of your custom ones. Run heroku config to see what’s actually there. If you’re using the EU region, that’s probably your issue. I had to change from api.mailgun.net to api.eu.mailgun.net in my config. Also check that mailgun-js is in your package.json and Heroku’s actually installing it. Try clearing the build cache: heroku plugins:install heroku-builds then heroku builds:cache:purge. That fixes most deployment weirdness.