How to send email in Laravel 11

Created at 13-Mar-2024 , By samar

To send email in laravel 11 you have to first install laravel 11 application using composer create-project laravel/laravel demo command. Now can follow below instructions step by step to send a email in Laravel 11.

Add mail configuration deatils in your .env file. Basically we have to add Outgoing Server details for MAIL_HOST and port and mail_encryption as per your mail account details.

MAIL_MAILER=smtp
MAIL_HOST=mail.yourwebsite.com
MAIL_PORT=465
MAIL_USERNAME=mail_username
MAIL_PASSWORD=mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mail_from_address"
MAIL_FROM_NAME="${APP_NAME}"

Create mail using php artisan make:mail TestMail command and it will a file app\Mail\TestMail.php in your laravel project. Paste below code in 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;
use App\Models\User;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(public User $user)
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Test Mail',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'mail.test',
            with: [
                'userName' => $this->user->name,
                'userEmail' => $this->user->email,
            ],
        );
    }

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

Create a controller named TestController in /app/Http/Controllers directory and paste below code in it. You can also create controller using php artisan make:controller TestController command and make changes in file as per below code. You have atleast one user in your users table and enter your email address in place of testing@example.com in Mail::to('testing@example.com')->send(new TestMail($user));.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;

class TestController extends Controller
{
    //

    public function sendEmail(){
        $user = User::first();
        try{
            $mail =    Mail::to('testing@example.com')->send(new TestMail($user));
            echo "Mail has been sent successfully";
        }catch(\Exception $e){
            echo $e->getMessage();
            Log::info(['Mal error on account approval'=> 'Mail not sent successfully']);
        }
    }
}

Add below code in your web.php file under routes directory

use App\Http\Controllers\TestController;
Route::get('/test-mail', [TestController::class, 'sendEmail'])->name('sendEmail');

Create a test.blade.php file in resources/views/mail directory and place below code in it.

<div>
    Name: {{ $userName }}
    Email: {{ $userEmail }}
</div>

Now you can visit https://yourwebsite.com/test-mail URL to see the output.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.