In Laravel 9+, you can configure the sender of the email in two ways:
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',
);
}
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.
2
Answers
In Laravel 9+, you can configure the sender of the email in two ways:
address by returning a new
IlluminateMailMailablesEnvelope
instance from theenvelope()
method in your
Mailable
class."from" address for all of its emails, you can define a global "from"
address in your
config/mail.php
configuration file. This addresswill be used if no other "from" address is specified within the
Mailable
class.If you are using a version of Laravel 8 or lower it is otherwise, in the
Mailable
class you can do it like thisYou 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.
Edit config/mail.php:
and set the from like this: