Set email headers so bounced emails go to a specific address

Max Williams picture Max Williams · Mar 14, 2011 · Viewed 47.5k times · Source

From our rails app we send out some system-generated emails with the 'from' address set to [email protected]. If these bounce they get sent back to this address by our mail server. However, what i'd like to do is to not have bounced emails get sent back to [email protected] but to a different address, such as [email protected].

Is there a header or something i can set in the email that will achieve this, without me having to go and investigate the vagaries of our email server? We send the mails out using exim in case that's relevant.

cheers, max

Answer

ColinM picture ColinM · Aug 13, 2011

I just figured this out myself in exim4 after a lot of reading about exim configuration.

First, you want your app to add the following header:

Return-Path: <[email protected]>

Works with or without brackets. Exim will add brackets in the end either way.

Second, this was the hard part. Exim always wanted to override my Return-Path: address with the unix user who sent it. You can use /etc/email-addresses in Ubuntu to set a static email for the user of your web app, but this still ignores the Return-Path header. Here is how I modified my exim config to respect the Return-Path from the web app:

In the main config area add:

return_path_remove = false

In the appropriate router config (e.g. dnslookup):

dnslookup:
  # ...
  errors_to = ${if def:h_return-path: {${address:$h_return-path:}} fail}
  headers_remove = return-path
  no_more

Now exim should copy the Return-Path header address at the envelope-level and delete the original Return-Path header.

I tried lots of other configuration directives and this is the only way that actually worked for me.