Ben Borgers / Posts /

How to set a Return-Path header for Laravel mail

January 27, 2021 by @benborgers

The Return-Path header in an email defines which email address should receive bounces and complaints (such as getting marked as spam). It's useful to set this header so you can monitor how many complaints you're getting, and which emails are bouncing.

You can set this header in a Laravel Mailable using the withSwiftMessage() method:

public function build()
{
    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()
                ->addTextHeader('Return-Path', 'email@example.com');
    });

    return $this
        ->view('some.view');
        // the rest of the normal mailable methods for from, subject, etc
}

Now, this email will have the Return-Path header defined, and receiving email clients will send a notification email to that address if the email bounces or is marked as spam.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!

Loading...