In this tutorial, I will show you how to send emails in Laravel 12 using the built-in Mail feature. Sending emails is an essential feature in web applications, whether for user notifications, password resets, or contact forms.
We will go through the step-by-step process, including configuring the mail server, creating a Mailable class, and sending emails using Laravel’s Mail facade.
Step-by-Step Guide to Sending Email in Laravel 12
If you haven’t installed Laravel 12 yet, run the following command:
laravel new laravel-12-send-email
Open the .env file and update the email configuration:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your-email-password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="Your App Name"
Note: If you are using Gmail SMTP, you may need to enable Less Secure Apps or generate an App Password for authentication.
Run the following command to generate a new Mailable class:
php artisan make:mail SendEmail
This will create a file in app/Mail/SendEmail.php.
Open app/Mail/SendEmail.php and update it:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Send Email',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.send-email',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Create a new directory resources/views/emails/ and inside it, create a file send-email.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Email from Laravel 12</title>
</head>
<body>
<h2>Hello, {{ $data['name'] }}!</h2>
<p>{{ $data['message'] }}</p>
<p>Thank you!</p>
</body>
</html>
Run the following command to create a new controller:
php artisan make:controller EmailController
Now open app/Http/Controllers/EmailController.php and update it:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendEmail;
class EmailController extends Controller
{
public function sendEmail()
{
$data = [
'name' => 'John Doe',
'message' => 'This is a test email from Laravel 12.'
];
Mail::to('[email protected]')->send(new SendEmail($data));
return response()->json(['success' => 'Email sent successfully.']);
}
}
Open routes/web.php and add this route:
use App\Http\Controllers\EmailController;
Route::get('/send-email', [EmailController::class, 'sendEmail']);
Now, start your Laravel server:
php artisan serve
You might also like: