Laravel 11 Send Email Example

In Laravel 11, there is a built-in function to send mail that you can use with external SMTP drivers like Gmail, Mailgun, Postmark, Amazon SES in the mail facade to send emails.

In this example guide, we will send mail using Gmail SMTP driver with Laravel 11 application.

Let’s send email using gmail smtp:

Step 1 – Setup New Laravel 11 Project

To setup the new Laravel 11 application on the server you can use the given command:

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

Step 2 – Configure Mail Server

Open the .env file in the root directory of your Laravel application, And configure mail server details in it; Something like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls

To allow non-secure apps to access Gmail to send emails, you can go to your Gmail settings here and click Enable less secure apps.

Step 3 – Create Mail Class

Run the following command to create mail class, which is used to send mails

php artisan make:mail HelloMail

Once you have created HelloMail class, Then visit the app/mail directory and open HelloMail.php file and add the following code into it:

<?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 HelloMail 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: 'Welcome on tutsmake.com',
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'emails.helloMail',
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 4 – Define Route

Define a route in the web.php file to handle the request to send mail; Something like this:

use App\Http\Controllers\HelloController;

Route::get('hello-email', [HelloController::class, 'sendHelloMail']);

Step 5 – Create a Controller File

Run the following command to make a controller file to handle mail sending process into it:

php artisan make:controller HelloController

Open a HelloController.php controller file from app/http/controller folder and update the following code into it:

<?php

namespace App\Http\Controllers;

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

class HelloController extends Controller
{
public function sendHelloMail()
{
$title = 'Hello Mail from Tutsmake.com';
$body = 'This is the first email to send in laravel 11 application from tutsmake.com';

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

return "Email has been sent successfully!";
}
}

Step 6 – Create an Email View

Create a helloMail.blade.php file inside resources/views/emails folder and add the following HTML code into it:

<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<p>{{ $body }}</p>
</body>
</html>

Step 7 – Test Application

Run php artisan serve command to start application server to testing:

php artisan serve

Open browser with URL http://127.0.0.1:8000/hello-email to send first hello mail from laravel 11 application.

Conclusion

Congratulations! In this example, you taught how to send the first hello mail in Laravel 11 application using Mailable class with gmail smtp.

Recommended Guides

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *