Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ->replyTo() in mailable not overwriting global 'reply_to' setting

In config/mail.php, we have:

'reply_to' => [
    'address' => env('MAIL_REPLY_TO_ADDRESS', '[email protected]'),
    'name' => env('MAIL_REPLY_TO_NAME', 'Company')
],

And the mailable looks like this:

namespace App\Mail;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SupportMessage extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $user;
    public $senderEmail;
    public $message;

    public function __construct(User $user, $email, $message)
    {
        $this->user = $user;
        $this->senderEmail = $email;
        $this->message = $message;
    }

    public function build()
    {
        return $this->markdown('emails.support-message')
            ->subject('Support Message')
            ->replyTo(['email' => $this->senderEmail]);
    }
}

For some reason, instead of replacing the default reply-to header in the email, Laravel concatenates $this->senderEmail onto the existing [email protected], which email clients don't seem to be responding to (blank email list when replying). The header comes through looking something like this: reply-to: Company <[email protected]>, [email protected]

I have also tried ->replyTo($this->senderEmail), which results in the same concatenation.

Is there a way to replace the global reply-to rather than concatenating?

like image 869
Josh Mountain Avatar asked Oct 24 '25 14:10

Josh Mountain


1 Answers

Try this way to override the global 'reply_to' setting. This is tested in Laravel 5.8 . Thanks @miken32 for the hackish solution.

Just save the below code in app\Mail\MailableExtend.php and make App\Mail\SupportMessage extend the MailableExtend class, instead of Laravel's built-in mailable.

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable as MailableBase;

class MailableExtend extends MailableBase
{

    protected function buildRecipients($message)
    {
        parent::buildRecipients($message);

        if (count($this->replyTo)) {
            //erase any existing reply-to header
            $message->getHeaders()->remove('reply-to');

            //add re-apply just ours
            foreach ($this->replyTo as $recipient) {
                $message->replyTo($recipient['address'], $recipient['name']);
            }
        }

        return $this;
    }

}
like image 178
Suvash sarker Avatar answered Oct 27 '25 05:10

Suvash sarker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!