phpphpmailerdirectadmincustom-headers

PHPmailer custom header set, but not shown on delivery


I am using PHPMailer6.2.0 and I am having issues setting the return path.

I have added the custom header via PHPmailer function addCustomHeader()

$mail->addCustomHeader("Return-Path", $fromemail);

and for debugging I have printed out the header content in \PHPMailer\PHPMailer.php function mailSend($header, $body) on line 1794;

var_export($header);
die();

this prints out the header content before it will be sent and it verifies that the custom header return-path is set correctly, however in action, when i receive an email to my outlook, the header return path callbacks to the domains default email user@domain.com. Perhaps this is not the last place before the email is sent and it gets lost later on?

I am using DirectAdmin as my server manager


Solution

  • Stop right there! Senders should not set a return-path header. That header is added by the receiver, and what goes into it is dependent on the SMTP envelope sender, the address that's used in the SMTP MAIL FROM command that delivered the message. Setting this header as a sender is a straightforward contravention of the RFCs. So what should you do instead? Set the envelope sender, and in PHPMailer you do that like this:

    $mail->Sender = $fromemail;
    

    Even when you do this, whether the server your'e sending through will accept it is a different matter. For example gmail will not allow you to use anything other than your account username address or predefined aliases, not arbitrary addresses.