Set email headers so bounced emails go to a specif

2019-02-21 06:49发布

问题:

From our rails app we send out some system-generated emails with the 'from' address set to noreply@ourdomain.com. 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 noreply@ourdomain.com but to a different address, such as bounced@ourdomain.com.

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

回答1:

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: <bounced@yourdomain.com>

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.



回答2:

Errors-To is deprecated, so mail servers will typically ignore this header - most servers will bounce will to the 'envelope sender'.

This is the email address that your mail client sends as part of the connection to the SMTP server (not necessarily the From address - though it typically is the same).

I don't know Rails all that well, but I found this - although, as far as I can tell Return-Path is reset by MTAs to match the MAIL FROM information from the client, so it seems you can't actually set it.

I think the only thing you can do is set the bounce address in your server.



回答3:

3 years too late, but just in case anyone else comes this way. Return-Path is the right header but, as James Garriss pointed out above, it has to be placed there by the site performing final delivery. You can't just stick it in yourself.

If you're writing emails by connecting directly to an SMTP server then this is easy - the MAIL command contains the return path. If you send

MAIL FROM:<me@foo.com>

to the SMTP server then bounces will be returned to me@foo.com.

If you're not constructing SMTP, and you're running an MTA (ie. exim/etc), then you have to find a command-line switch for your MTA. For sendmail, -f me@foo.com "sets the sender's address", and this ends up as the Return-Path in the final delivered mail, and me@foo.com will get the bounces (I do exactly this for auto-generated emails). I haven't tried this on exim, but it has exactly the same option, and it should work.



回答4:

Here is the solution:

In the email header you can set:

From: "From Name" <from_name@ourdomain.com>
Reply-To: noreply@ourdomain.com

Errors-To: <bounced@ourdomain.com>
Return-Path: <bounced@ourdomain.com>


回答5:

Return-Path header is written by the receiving server, not by the sending server. And as per the RFC 5321, it is the same as the address supplied in MAIL FROM command.

Even if you set the Return-Path header yourself, the receiving server will overwrite that.

Now, here's the thing, the address in the MAIL FROM command and the address in the From header can be different. The receiving user does not see the MAIL FROM address. They only see the From header address.

So, if you want to ignore the bounces or want them to go to a specific address, you should use that address in the MAIL FROM command.

But in the From header, you can just use noreply@yourdomain.com - the user will see this address.


To simplify a bit more, you send the email from handle_bounce@yourdomain.com address. The receiving server will send the bounces to this address.

To show your user the noreply@yourdomain.com address instead of handle_bounce... address, set the From header in the raw email MIME message to the noreply... address.


I recently got a no-reply email from Bitbucket. Here's the raw message:

Return-Path: <bounce-1231860_HTML-1209402755-103116181-132689-225@bounce.mailer.atlassian.com>
From: "Atlassian Bitbucket" <noreply@mailer.bitbucket.org>
To: <me@hostname.com>
Subject: Continuous delivery, without the headache.
Date: Wed, 28 Feb 2018 12:40:53 -0600
MIME-Version: 1.0
Reply-To: "Atlassian Bitbucket" <reply-fe3915707665057b741c71-1231860_HTML-1209402755-132689-225@mailer.atlassian.com>

... message body ...

As you can see, the Return-Path is an address dedicated to handle bounces. But the From address is a noreply@... mail. What that means is this email was actually sent by this bounce handling address, not by the noreply address.

You can also see the Reply-To header, which is dedicated to handle replies, if a user replies to no-reply emails. Those replies are probably discarded right away.