Problem/Motivation
Drupal's mail command sets the Return-Path
header directly. This is in violation of the relevant RFCs.
https://tools.ietf.org/html/rfc2821#section-4.4
A message-originating SMTP system SHOULD NOT send a message that
already contains a Return-path header. SMTP servers performing a
relay function MUST NOT inspect the message data, and especially not
to the extent needed to determine if Return-path headers are present.
SMTP servers making final delivery MAY remove Return-path headers
before adding their own.
More recent RFCs:
The Return-Path
header is set by the SMTP server to the value of the envelope sender (MAIL FROM:
SMTP command). When using sendmail or other local MTA this can often be set using the "-f" option.
When Drupal sets the Return-Path
header the recipient mail server may reject the email or ignore the header. If the email gets through, it results in 2 headers which can be different and which the recipient might flag as spam.
External mail services, such as Mailgun or Sendgrid, set their own envelope sender (therefore Return-Path
header) to capture any bounces. Drupal should not try to influence the header at all.
External SMTP services may use Variable Envelope Return Path.
Proposed resolution
Do not set the "Return-Path" header within Drupal and provide more detailed documentation on Drupal's mail system.
That is, remove Return-Path
altogether and set the envelope sender to $message['from']
. See https://api.drupal.org/api/drupal/core!core.api.php/function/hook_mail_a...
PHPMailer has resolved the issue: