2

If I just add the header in the mailable:

public function headers()
{
    return new Headers(
        text: [
            'Return-Path' => config('mail.from.address'),
        ],
    );
}

I get an error:

The "Return-Path" header must be an instance of "Symfony\Component\Mime\Header\PathHeader" (got "Symfony\Component\Mime\Header\UnstructuredHeader").

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180

1 Answers1

2

Only solution I found was with "using" in Envelope:

public function envelope()
{
    return new Envelope(
        using: [
            function (Email $message) {
                $message->getHeaders()->addHeader('Return-Path', config('mail.from.address'));
            },
        ]
    );
}

That works for me.

I also tried to add a name:

use Symfony\Component\Mime\Address as SymfonyAddress;
$message->getHeaders()->addHeader('Return-Path', new SymfonyAddress(config('mail.from.address'), config('mail.from.name')));

But that creates an invalid result:

Return-Path: <"Some Name" <no-reply@someaddress.com>>

I guess name is not supported here?

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • I believe that's correct: names are not supported in the return-path. The intention for return-path is that it is a machine readable email address to send bounce, and other automated notifications to, and shouldn't be visible to users. The reply-to address will be the user-visible variant. Often these two values are the same email address, but being able to set the return path separately is important. https://mxtoolbox.com/dmarc/spf/setup/spf-return-path Thanks for your answer! It really helped me get things configured to prevent bounced mail notifications from flooding our inbox! – Kevin Foster Jul 20 '23 at 01:19