skip to Main Content

I want to be able to set a different ‘Sender’ to the ‘From added’ in Laravel emails. Is this possible?

2

Answers


  1. In Laravel 9+, you can configure the sender of the email in two ways:

    1. Using the Envelope: You can specify the "from" address and "replyTo"
      address by returning a new
      IlluminateMailMailablesEnvelope instance from the envelope()
      method in your Mailable class.
    use IlluminateMailMailablesAddress;
    use IlluminateMailMailablesEnvelope;
    
    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address('[email protected]', 'John Doe'),
            replyTo: [
                new Address('[email protected]', 'Jenny Doe'),
            ],
            subject: 'Order Created',
        );
    }
    
    1. Using a Global "from" address: If your application uses the same
      "from" address for all of its emails, you can define a global "from"
      address in your config/mail.php configuration file. This address
      will be used if no other "from" address is specified within the
      Mailable class.
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],
    
    In addition, you may define a global "reply_to" address within your `config/mail.php` configuration file:
    
    'reply_to' => ['address' => '[email protected]', 'name' => 'App Name'],
    

    If you are using a version of Laravel 8 or lower it is otherwise, in the Mailable class you can do it like this

    public function build()
    {
        return $this->from('[email protected]')
                    ->sender('[email protected]', 'Jenny')
                    ->view('emails.order-created');
    }
    

    You can also use the global option of the configuration file in previous versions of Laravel 9.

    You can get more information in the Laravel documentation.

    Login or Signup to reply.
  2. Edit config/mail.php:
    and set the from like this:

    'from' => ['address' => '[email protected]', 'name' => 'Xelber Stackoverflow']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search