Fixing DirectAdmin Bounce Emails with Mailgun
Switching to Mailgun SMTP
It's been about two weeks since we've switched from our in-house email system to Mailgun SMTP services in order to improve delivery of our email. Not that we send many, no – we average just about 3000 emails a month, the majority of which are account signup emails, and invoices being sent to our customers. The configuration was easy and straightforward, and they always have someone available to help if you run into problems (We didn't!).
The Problem: Bounce Emails Leaving the Server
After having everything set up, we noticed a large number of internal emails were getting routed via Mailgun and, subsequently bounced. Oops! That shouldn't happen. Those emails were never supposed to leave our server.
A little search on Google revealed that DirectAdmin, the control panel we use, was trying to deliver bounce emails – the ones coming from MAILER-DAEMON – to a local mailbox using remote SMTP. This needed to be fixed. We also run this panel on all our DirectAdmin Hosting plans, so we had to repeat this process on each server. First of all, it uses up our Mailgun email quota, and it just simply shouldn't happen. Email between internal accounts should be delivered internally without leaving the server at all. Let's start to fix this…
The Solution
Even the official DirectAdmin team's solution says that what we're about to do is 'not entirely correct in it's logic', we're going to go ahead and fix the problem. Problems are meant to be fixed.
DirectAdmin uses Exim to send email, and we've already configured everything to be passed on to Mailgun, which is why we will not cover that topic here, and will only focus on preventing bounce messages from leaving the server.
Step 1: Open the System Filter File
The first thing to do is to open the Exim system filter file. You can do this with your preferred text editor:
nano /etc/system_filter.eximOr use vi or vim if you prefer.
Step 2: Find the Existing Code
Look for this code block in the file:
if not first_delivery
then
finish
endifStep 3: Add the Bounce Prevention Code
Then paste this code block right before the code you found in Step 2:
if $sender_address is ""
then
if $header_Auto-Submitted: is not "auto-replied"
then
if ${lookup{${extract{2}{@}{$recipients}}}lsearch{/etc/virtual/domains}{yes}{no}} is "no"
then
noerror fail text "Delayed bounce message ignored"
seen finish
endif
endif
endifStep 4: Save and Restart Exim
Save the file and restart Exim using one of these commands:
service exim restartOr:
/etc/init.d/exim restartUse whichever command is available on your system.
Important Notes
The above instructions were done on a CentOS 6 system. If you run a different Linux distribution (such as Ubuntu, Debian, or CentOS 7+), modify the commands accordingly. The Exim configuration syntax remains the same across distributions.
Why This Fix Works
This configuration prevents bounce messages (emails with empty sender addresses from MAILER-DAEMON) from being sent through the remote SMTP server (Mailgun). Instead, they are handled locally, which:
- Prevents unnecessary usage of your Mailgun email quota
- Keeps internal server communications internal
- Reduces potential email delivery issues
- Improves overall email system efficiency
Conclusion
By implementing this fix, you'll ensure that bounce emails stay on your server where they belong, rather than being unnecessarily routed through your Mailgun SMTP service. This is especially important if you're managing multiple DirectAdmin servers, as you'll need to apply this fix to each one.