How do I redirect DSN / NDR / Bounce emails to another email address?
To change the destination of bounce emails you need to modify the 'envelope' from address.
If a message cannot be delivered we send a message back to you explaining why - this is called a delivery status notification (DSN) or non-delivery report (NDR), for short they are called 'bounce' emails.
Email messages have a number of elements that you need to be aware of when considering how to handle bounces:
- ENVELOPE FROM
- This is the from email address declared as the 'MAIL FROM' when your email application initially connects.
- Bounce emails will always be sent back to this address.
- RETURN PATH
- This address is stored in the email headers as 'Return-Path:' and will always be set based on the 'ENVELOPE FROM' address.
- You cannot manually specify this address, it will be over-written with the 'ENVELOPE FROM' address during delivery.
- HEADER FROM
- This address is stored in the email headers as 'From:' and is what the recipient will see as the from address.
- This address is stored in the headers of your emails as 'Reply-To:'
- This is optional but if specified it is the address the recipients reply will go to.
If you have a central email server or custom application it is possible to modify the ENVELOPE FROM address so that you can redirect bounce messages to an alternative address for processing. It is not possible with email programs such as Outlook or Mac Mail.
It is important to note that both the ENVELOPE FROM and the HEADER FROM address have to be authorized on your account for sending.
Example SMTP Transaction
The following image demonstrates a simple SMTP transaction highlighting when the different addresses are declared.
Bounce Message Format
Bounce messages always use a standard format so that you can easily identify them:
To prevent mail loops, bounce messages never have a RETURN PATH or ENVELOPE FROM address set.
As an example for the message body, if you sent a message to an invalid email address it would contain something like:
Following that would be the headers of the original message that could not be delivered.
Modifying the ENVELOPE FROM address
The method of modifying the envelope from address will vary depending on your application and how you are sending email but below you will find some guidance for the most popular methods of sending email.
PHP mail() function
When using the standard PHP mail() function you can pass the ENVELOPE FROM in the 5th argument (additional parameters), here is a code example:
IMPORTANT: This code is provided for informational purposes only, the standard PHP mail() function does not currently support SMTP authentication so cannot be used with AuthSMTP.
PHPMailer is a PHP library that offers a range of features for sending email from PHP applications. (PHPMailer setup guide).
To modify the ENVELOPE FROM in PHPMailer you have to add an additional 'Sender' parameter:
...so your code would look something like:
For a full code example please see our PHPMailer setup guide.
PEAR is framework and distribution system for reusable PHP components.
There are two main methods of sending email using PEAR::Mail, you can either use the default PHP mail() function which will send via the local sendmail daemon or you can connect directly to an SMTP server.
PEAR::Mail via Local Sendmail
If you have your local sendmail daemon configured with AuthSMTP as a smart host you can use the following command to send an email with an alternative ENVELOPE FROM address:
PEAR::Mail via SMTP Server
If you are using PEAR::Mail directly with your AuthSMTP account as an SMTP server (as per our PHP PEAR::Mail Setup Guide) you will need to specify the ENVELOPE FROM address in the 'Return-Path' header, this is added via the $headers array.
For a full code example please see our PHP PEAR::Mail Setup Guide.
Sendmail would not normally be invoked directly for sending email but it is possible, you would create a text file using the following text:
...save this to a text file (message.txt) and run the following command:
...and you will see the message arrive from 'email@example.com' or possibly 'firstname.lastname@example.org', if you send it to an invalid email address (e.g. email@example.com) the bounces will also go back to 'firstname.lastname@example.org'.
However, if you instead run:
..the message will still appear to come from 'email@example.com' but any bounce will go back to 'firstname.lastname@example.org'.
The standard PHP mail() program invokes the local sendmail daemon and uses this same '-f' parameter to set the ENVELOPE FROM.
If you are using Sendmail as the mailer agent on a central server or hosting service and you have it configured to use our service as a smart host (Sendmail smart host setup guide) you will need to set the ENVELOPE FROM address in the application that is submitting the email to Sendmail, as far as we are aware you cannot configure Sendmail itself to overwrite / rewrite the ENVELOPE FROM address only, during the delivery process.
Postfix is a mailer agent much like Sendmail, it includes an interface / alias so that you can run the same Sendmail commands on a server with Postfix installed instead of Sendmail itself. Please see the example in the Sendmail section above.
However, Postfix does provide some functions so that you can overwrite / modify the ENVELOPE FROM address during delivery - this is useful if you cannot modify the ENVELOPE FROM address in your application or you have multiple sources of email using different from addresses.
To setup ENVELOPE FROM rewriting add the following configuration to '/etc/postfix/main.cf':
...ensuring that there are no duplicate configurations.
- The first line tells Postfix to only rewrite the ENVELOPE FROM address
- The second line tells Postfix where to read the rewrite mappings from
You will then need to add some entries to the mapping file '/etc/postfix/canonical' to tell Postfix what to rewrite from / to. In this example we will rewrite an ENVELOPE FROM of 'email@example.com' to 'firstname.lastname@example.org':
You can also do wildcard rewrites:
IMPORTANT: Don't forget to run 'postmap' on your mapping file and restart the Postfix service for the changes to take effect.
In all of these examples your message will still appear to the recipient as from 'email@example.com' but any bounces will go directly back to 'firstname.lastname@example.org'.
In you are sending high volumes of email from your own server / network and wish to automate the handling of the bounce messages, Postfix offers more advanced rewriting options that can be utilised as part of the automation process.
To do this please edit the /etc/postfix/main.conf file and replace 'hash:/etc/postfix/canonical' with 'regexp:/etc/postfix/canonical_regexp'
You will then need to create the /etc/postfix/canonical_regexp file and enter a regular expression based rewrite mapping rule. As an example we will take the following example situation.
Your organisation operates multiple websites from https://www.example1.com, https://www.example2.com and https://www.example3.com (on a server with Postfix installed) and you send your website emails from those domain names but you operate your internal network on the example-internal.com domain name and wish to direct the bounce messages to that domain name for automated processing.
This example mapping will convert 'email@example.com' to 'firstname.lastname@example.org':
Postfix offers many different features for the manipulation of messages and you can find further details in their documentation.
If you have any questions about bounce messages generated by messages sent through your AuthSMTP account please contact us.