Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osxtra's avatar
Level 1

Return-Path custom header breaks mailable

Laravel v11.39.1

Hi, all, this may be more a Symfony question, but curious if anyone knows why adding a Return-Path custom header to a mailable fails, whereas other custom headers do not.

This is a handy header to have in case of bounces, in case you don't want them going back to your main reply-to address (say a company whose main address is read by a bunch of folks, but a bounce should go to a different department).

Following the docs at https://laravel.com/docs/11.x/mail#headers, adding a headers method to the mailable works for any custom header (except return path, which produces an error):

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

(And yes, have tried ['return-path' => '[email protected]'] as well as 'Return-Path')

Per https://stackoverflow.com/questions/74995900/howto-set-return-path-in-laravel-9-symfony-mailer

Adding a call to the build method of the mailable works: $this->withSymfonyMessage(function (Email $message) { $message->getHeaders()->addPathHeader('Return-Path', "EMAIL_ADDRESS_HERE"); });

A note was in the post saying it still works in v11, so was glad to find it, but curious why that one specific header is crashing the mailable. Return-Path is present in some of Symfony's mailer classes, so not sure what's going on.

Anyone (besides the 'bot) have an idea? (No offense, Larry, just prefer human replies.)

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

In Laravel, when dealing with mailables and custom headers, the Return-Path header is a special case because it requires a specific type of header object in Symfony's Mime component. The error you're encountering is due to the fact that the Return-Path header must be an instance of Symfony\Component\Mime\Header\PathHeader, rather than a generic UnstructuredHeader.

To set the Return-Path header correctly, you need to use the withSymfonyMessage method to directly manipulate the underlying Symfony Email object. Here's how you can do it:

use Illuminate\Mail\Mailable;
use Symfony\Component\Mime\Email;

class YourMailable extends Mailable
{
    public function build()
    {
        $this->view('emails.your_view');

        $this->withSymfonyMessage(function (Email $message) {
            $message->getHeaders()->addPathHeader('Return-Path', '[email protected]');
        });

        return $this;
    }
}

Explanation:

  • withSymfonyMessage: This method allows you to access the underlying Symfony Email object, which gives you more control over the email headers.
  • addPathHeader: This method is used to add a PathHeader, which is the correct type for the Return-Path header.

The reason other headers work without issue is that they can be represented as UnstructuredHeader, which is the default type for most headers. However, Return-Path is a special header that requires a specific format, hence the need for PathHeader.

By using the withSymfonyMessage method, you ensure that the Return-Path header is set correctly, avoiding the LogicException you encountered.

osxtra's avatar
Level 1

So, this time Larry got it right. Good to know!

Since return-path is an often-used header, am wondering if either the docs should be updated to include this info, or Laravel just automagically look for it when the headers method as shown in the docs is employed and munge as needed...

Please or to participate in this conversation.