Code your dreams into reality.
Every line of code is a step towards a better future.
Embrace the bugs, they make you a better debugger.

Laravel 11 Send Email Example Tutorial

Last Updated on April 11, 2024 by

In this tutorial, i will show 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 1: Install Laravel

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

composer create-project --prefer-dist laravel/laravel SendEmailProject

Step 2: Configure Email Settings

Navigate to the .env file in your Laravel project and configure your email settings according to your email provider; like the following:

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

To create a new Mailable class using Laravel’s Artisan command:

php artisan make:mail FirstEmailSend

Step 4: Configure the Mailable

In the App\Mail\FirstEmailSend.php file, define the email content and subject within the content() method:

<?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

By default email templates in the resources/views/emails folder. Create a myFirstEmail.blade.php Blade template in it:

<!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

Create a new controller using the following Artisan command:

php artisan make:controller WelcomeController

Define a method for sending the email in app/http/controllers/WelcomeController.php file ; like the following:

<?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 email from itcodStuff.com';

        Mail::to('[email protected]')->send(new FirstEmailSend($title, $body));

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

Step 7: Define Routes

Open the routes/web.php file and add the following route definition:

use App\Http\Controllers\WelcomeController;

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

Step 8: Testing

To test the laravel 11 send email application, run this application by using php artisan serve command:

php artisan serve

And then trigger the route http://127.0.0.1:8000/send-first-email on browser that sends the email. Check the recipient’s inbox for the email.

That’s it! You’ve successfully sent your first email in Laravel 11 application.

Reference by Laravel 11.x Mail:- https://laravel.com/docs/11.x/mail.

Leave a Comment