How To Send Email With Dynamic Sender Address In Laravel?

Hello artisan,

You know that we can send emails in Laravel very easily.  But in this tutorial, I will show you how to override the default from email address in laravel. If we define MAIL_FROM_ADDRESS in our .env file then laravel automatically take it as sender's email address. But in this example, I will show you how we can send email with dynamic sender in laravel.

We can pass callback to Mail facades to make it dynamic for sender mail address. So from this tutorial, you will learn how to config laravel dynamic email sender.

If we use the below method to send mail, then there is no option to make the sender address dynamic. 

Mail::to($request->user())->send(new MailableClass);


See the below example code to override the default from the email address in laravel and config laravel dynamic email sender easily.

$data['name'] = auth()->user()->name;
$data['email'] = $request->to;
$data['subject'] = $request->subject;
$data['mgs'] = $request->body;
Mail::send('email.mail', $data, function($message) use ($data) {
   $message->to($data['email'], $data['name'])->subject($data['subject']. $data['name']);
   $message->from(auth()->user()->email, $data['name']);


Read also: How to Change Mail Driver Dynamically in Laravel


Hope it can help you.


#laravel #laravel-9x #laravel-mail