Howto set Return-Path in Laravel 9 (Symfony Mailer)

942 Views Asked by At

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").

1

There are 1 best solutions below

1
On

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" <[email protected]>>

I guess name is not supported here?