1090 0 0 0
Last Updated : 2025-04-28 21:14:57
this snippet will teach you how to set different FROM EMAIL which exists in database to use as sender email when sending emails in laravel
after creating mailable file through this code
php artisan make:mail [wanted name of mail.php file] -m [specified view for blade file of the email itself]
// for example
php artisan make:mail [welcomeMail] -m [emails.welcome]
the command above will create a welcomeMail.php in app\Mail folder and welcome.blade.php in resources\views\emails folder
2- now you should modify build function to get email record from database and use it as sender's email in welcomeMail.php like this
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\MessagesSetting ; // use this line to define model name
class newMessage extends Mailable
{
use Queueable, SerializesModels;
public $data ;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data)
{
$this->email = $data['email'];
$this->subject = $data['subject'];
$this->message = $data['message'];
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
// use next 2 lines to get the email you want to set as sender's email in your application
$messagesSettings = MessagesSetting::find(1);
$fromMail = $messagesSettings->email;
return $this->markdown('emails.newMessage')
->from($fromMail) // use this line to determine the from email
-> with ([
'subject' => $this->subject ,
'message' => $this->message ,
]);
}
}