How to Send Email in Laravel 9
Laravel 9 e-mail sending tutorail; on this tutorial we can learn how to send emails in laravel 9 the usage of SMTP drivers like Mailgun, Postmark, Amazon SES and sendemail.
Laravel 9 provide us a mail class for sending emails. So we would really like to show you the way to send emails from localhost the use of mailable in laravel 9 project.
Laravel 9 affords numerous SMTP drivers consisting of Mailgun, Postmark, Amazon SES and sendmail.
This is a simple way to send multiple emails from localhost. It also work on live server.
How to Send Email in Laravel 9
Following steps are required to send emails from localhost in laravel 9 project:
-
Step 1 – Laravel 9 App creating
-
Step 2 – SMTP Configuration in .env file
-
Step 3 – Mailable Class creating
-
Step 4 – Email Send Route creating
-
Step 5 – View files creating
-
Step 6 – Email Controller creating
-
Step 7 – Testing project
Step 1 – Laravel 9 App Creating
First of all we need a fresh project of laravel 9. run the given comman to create a new project:
composer create-project laravel/laravel EmailSend
Step 2 – SMTP Configuration in .env file
Open your project on your favourite code editor and open file .env and replace the given code.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=enter your email which you want to use for mail sent
MAIL_PASSWORD=enter password of email which you using
MAIL_ENCRYPTION=tls
If you using Gmail to send a mail then you need to turn of Less Secure in google account setting. Click here to update your setting.
Step 3 – Mailable Class Creating
We need a NotifyMail class which is creating by running given command.
Php artisan make:mail NotifyMail
After creating NotifyMail update with the given code or you can simply write in build() function:
return $this>view('MailSend');
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotifyMail extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this>view('MailSend');
}
}
Step 4 – Email send route creating
In your project directory open routes/web.php file and paste the given code in it.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmailController;
Route::get('emailsend', [EmailController::class, 'index']);
Step 5 – View files creating
So in this we will see how to create view files which are use to send mail. Open resouces/views directory then create a files with name MailSend.blade.php and paste given code in it and save.
<!DOCTYPE html>
<html>
<head>
<title>How to Send Email in Laravel 9</title>
</head>
<body>
<h2>TheSkillStock – Email Sending Testing</h2>
<p>How to Send Email in Laravel 9</p>
</body>
</html>
Step 6 – Email Controller Creating
So the main step in this tutorial is a EmailController which provide functionality to send email to a user. Run the given command to create controller:
php artisan make:controller EmailController
So in your project directory open App/Http/controllers and open EmailController.php and replace with the given code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\NotifyMail;
class EmailController extends Controller
{
public function index()
{
Mail::to('ReciverEmail')->send(new NotifyMail());//replace and email which going to recive a email with ReciverEmail
if (Mail::failures()) {
return response()->Fail('Sorry! Please try again latter');
}else{
return response()->success('Successfully send your mail');
}
}
}
Step 7 – Testing project
So the final step is to test our project by running this command.
php artisan serve
Click here to open your project:
Run project http://127.0.0.1:8000/
Thank You For reading this tutorial