How to change send email address using laravel mail
In Laravel, you can change the sender email address used by the Mail
facade or the Illuminate\Support\Facades\Mail
class by setting the from
property on the Mail
instance or the Illuminate\Mail\Message
instance.
Here are a few ways to do it:
Method 1: Using the from
method on the Mail
facade
You can use the from
method on the Mail
facade to set the sender email address:
Mail::to('[email protected]')->from('[email protected]', 'New Sender Name')->send();
In this example, the email will be sent from [email protected]
with the name New Sender Name
.
Method 2: Using the from
method on the Illuminate\Mail\Message
instance
You can also set the sender email address on the Illuminate\Mail\Message
instance:
$message = (new Message())->from('[email protected]', 'New Sender Name');
Mail::to('[email protected]')->send($message);
Method 3: Setting the from
property on the Mail
instance
You can set the sender email address on the Mail
instance using the from
property:
$mail = (new Mail())->from('[email protected]', 'New Sender Name');
$mail->to('[email protected]')->send();
Method 4: Using a custom from
method on a mail driver
If you're using a custom mail driver, you can override the from
method to set the sender email address. For example, if you're using the Illuminate\Mail\SwiftMailer
driver, you can override the from
method like this:
public function from($address, $name = null)
{
// Set the sender email address
return parent::from($address, $name);
}
Note that the from
method is called on the SwiftMailer
instance, so you need to call the parent method to ensure that the email is sent correctly.