MailGun Postfix

We use Postfix with Mailgun as our relay host. We came across an issue the other day where mail that was being sent and received by the same domain was bouncing. Below is what happened and how we fixed it.

The Issue

Mail being sent and received by the website domain was being rejected. Looking at the mailgun logs we noticed we the messages were bouncing with an error code 553 (take a look at: for similar error messages). To compound the issue, everytime it bounched, the receiver was flagged in the Bounches.

What's Happening?

The mail server on the recieving end is reporting that it doesn't have any knowlege of the user sending the email.

The Solution

There are several options:

  1. Turn off sender address verification.
  2. Add "x-mailgun-native-send: true" to the header of your message to turn off sender address rewriting.
  3. Send from a subdomain of ""... Perhaps "".(Warning, all outbound emails will display as "". Change the 4. MX records to point to Mailgun instead. (Warning, don't do this unless you have a route setup to route back to the internal server.)
  4. Change the DKIM record to instead of the custom DKIM record. (Warning, doing so may result in you seeing "sent via")

Because we didn't have access to their mail host, we decided to go with option 2. We modified our postfix (in to use header_checks (more info here: The goal is to insert the x-mailgun-native-send header with every message. Here is how

  1. edit and append the following to: /etc/postfix/header_checks
# Prepends header to ALL outgoing email with a From address
/^From:/ PREPEND x-mailgun-native-send: true
  1. edit your /etc/postfix/ to include:
# The header_checks parameter specifies an optional table with patterns
# that each logical message header is matched against, including
# headers that span multiple physical lines.
# By default, these patterns also apply to MIME headers and to the
# headers of attached messages. With older Postfix versions, MIME and
# attached message headers were treated as body text.
# For details, see "man header_checks".
header_checks = regexp:/etc/postfix/header_checks
  1. Restart postfix

  2. Remember to remove any addresses that were listed under the bounces tab in your mailgun control panel.

  3. Try resending an email (below is an example in using the mail program):

mail -s "Test Message" -- -f

That should do it! If your still have problems, you may want to try one of the other options listed above.

blog comments powered by Disqus
· By: