Laravel 11 Send Email Example Tutorial

Laravel 11 send email example tutorial tutorial, I’ll guide you how to send email using default mail facade in laravel 11 applications. Let’s send emails in laravel 11, simply configuring email server, creating mail class, designing email template and sending email from application:

Step-by-Step Guide to Send Emails in Laravel 11

Step 1: Install Laravel

Start your terminal and run the following Composer command to install new laravel 11:

composer create-project --prefer-dist laravel/laravel send-email-app

Step 2: Configure Email Settings

Navigate to .env and configure your email settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_email@example.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@example.com
MAIL_FROM_NAME="${APP_NAME}"

Step 3: Create a Mailable Class

Generate a new Mailable class named FirstEmailSend:

php artisan make:mail FirstEmailSend

Step 4: Configure the Mailable In App\Mail\FirstEmailSend.php, define the email content and subject:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class FirstEmailSend extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(private string $title, private string $body)
{
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'First Email from ITCODSTUFF.COM',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.myFirstEmail',
with: [
'title' => $this->title,
'body' => $this->body,
],
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}

Step 5: Create Email Template

Create resources/views/emails/myFirstEmail.blade.php for the email template:

<!DOCTYPE html>
<html>
<head>
<title>{{ $subject }}</title>
</head>
<body>
<h1>Hello from ITCODSTUFF!</h1>
<p>This is a first email from itcodstuff.com.</p>
</body>
</html>

Step 6: Create Controller and Handle Send Email

Define a method in App\Http\Controllers\WelcomeController.php to send the email:

<?php

namespace App\Http\Controllers;

use App\Mail\FirstEmailSend;
use Illuminate\Support\Facades\Mail;

class WelcomeController extends Controller
{
public function sendMail()
{
$title = 'Send First Email From ITCODSTUFF.COM';
$body = 'This is the first email from itcodStuff.com';

Mail::to('your-recipient@domain.com')->send(new FirstEmailSend($title, $body));

return "First email has been sent successfully!";
}
}

Step 7: Define Routes

Add a route in routes/web.php to trigger the email send:

use App\Http\Controllers\WelcomeController;

Route::get('/send-first-email', [WelcomeController::class, 'sendMail']);

Step 8: Test your app

Run your Laravel application:

php artisan serve

Visit http://127.0.0.1:8000/send-first-email in your browser to send the email. Check the recipient’s inbox for the email.

You’ve successfully configured and sent your first email in Laravel 11 using the default mail facade. This setup ensures effective email communication from your Laravel applications, promoting efficient interaction with users.