How to change from in laravel mail
In Laravel, you can change the from address in the mail configuration file or programmatically when sending a mail.
Method 1: Changing the from address in the mail configuration file
You can change the from address in the config/mail.php
file. This file is located in the config
directory of your Laravel project.
In this file, you can update the from
key in the mail
array. For example:
'mail' => [
//...
'from' => [
'address' => '[email protected]',
'name' => 'New From Name',
],
//...
],
Method 2: Changing the from address programmatically
You can also change the from address programmatically when sending a mail using the Mail
facade or the Illuminate\Support\Facades\Mail
class.
Here's an example using the Mail
facade:
use Illuminate\Support\Facades\Mail;
Mail::to('[email protected]')->send(new MyMail());
In this example, you can pass an array with the from address as the second argument:
Mail::to('[email protected]')->from('[email protected]', 'New From Name')->send(new MyMail());
Alternatively, you can use the from
method on the Mail
facade:
Mail::to('[email protected]')->from('[email protected]', 'New From Name')->send(new MyMail());
Method 3: Changing the from address using a mail driver
If you're using a mail driver like Mailgun or Sendgrid, you can also change the from address in the driver's configuration file.
For example, if you're using Mailgun, you can update the from
key in the config/mailgun.php
file:
'mailgun' => [
//...
'from' => '[email protected]',
//...
],
Similarly, if you're using Sendgrid, you can update the from
key in the config/sendgrid.php
file:
'sendgrid' => [
//...
'from' => '[email protected]',
//...
],