skip to Main Content

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:

<?php

namespace AppMail;

use AppUser;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

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

    public $user;

    public $senderEmail;

    public $message;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user, $email, $message)
    {
        $this->user = $user;
        $this->senderEmail = $email;
        $this->message = $message;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    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?

2

Answers


  1. I would try to override function setAddress() in your mailable class, or if i’d have multiple emails, then create new class which extends mailable and in concerned email extend this new class.

    Something like this :

    protected function setAddress($address, $name = null, $property = 'to')
    {
        if (empty($address)) {
            return $this;
        }
    
        foreach ($this->addressesToArray($address, $name) as $recipient) {
            $recipient = $this->normalizeRecipient($recipient);
            if($this->property === 'replyTo'){
               $this->{$property} = [[
                  'name' => $recipient->name ?? null,
                  'address' => $recipient->email,
               ]];
            }
            else{
               $this->{$property}[] = [
                  'name' => $recipient->name ?? null,
                  'address' => $recipient->email,
               ];
            }
        }
    
        $this->{$property} = collect($this->{$property})
            ->reverse()
            ->unique('address')
            ->reverse()
            ->values()
            ->all();
    
        return $this;
    }
    
    Login or Signup to reply.
  2. Try to add a new Reply-To header using the addTextHeader() method.

    $message = $this->markdown('emails.support-message')->subject('Support Message');
    
    $message->getHeaders()->addTextHeader('Reply-To', $this->senderEmail);
            
    return $message;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search