That is what the env is for. DEV writes to the log, Test sends to Helo, Mailtrap or similar.
https://laravel.com/docs/11.x/mail#mail-and-local-development
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
We have 3 environments: local development, test server and the prod.
The local dev server cannot send emails.
The test can send emails but I want to set the email addresses manually and not to the actual recipients.
The prod should send emails to the actual recipients.
What I do right now is I have this function in each controller:
public function sendEmails($mailObject, $recipients)
{
$environment= config('app.env');
switch ($environment) {
case 'local':
Log::debug('Email sent');
break;
case 'test':
Mail::to('[email protected]')->send($mailObject);
break;
case 'production':
Mail::to($recipients)->send($mailObject);
break;
}
}
And the usage would be for example:
$mailObject = new \App\Mail\SomeMail();
$this->sendEmail($mailObject, $user->email);
The first thing I can do to improve it is to move this function to a Trait perhaps instead of having that in every Controller. But besides that - is there a better way to do that? Maybe there's already a built-in feature in Laravel for that?
Thanks
Please or to participate in this conversation.