In this article we will learn how to send mail in Laravel 8
Follow the below steps to send mail in laravel.
Step 1- Mail Configuration
In step first, we have to set mail configuration settings with mail driver in .env file like below example.
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=edu.teknowize@gmail.com #sender email id
MAIL_PASSWORD=xyz@12 #sender email password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=edu.teknowize@gmail.com
MAIL_FROM_NAME="Teknowize" #your app name
Step 2 – Create Male Class
In second step we will create TestMail class for mail sending.
Go to your terminal and simply run this below command.
php artisan make:mail TestMail
Output
Step 3 –Make controller
Create a new MailController.php controller
php artisan make:controller MailController
app \Http \Controllers\ MailController.php
<?php
namespace App\Http\Controllers;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
function send_mail(){
$email_id = 'edu@teknowize.com';
$msg = [
'title' => 'Hello! from Teknowize',
'body' => 'This is my test mail in Laravel 8'
];
Mail::to($email_id)->send(new TestMail($msg));
return' Mail send successfuly to '.$email_id ;
}
}
Step 4 – Create Route
routes\web.php
<?php
use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/
Route::get('send_mail', [MailController::class, 'send_mail']);
Step 5 – Create message template
In this step, open your resources/views folder then create a Email folder then create mail.blade.php in Email folder.
You can also create blade page in resources/views without making any folder.
resources \views \Email \mail.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>{{ $msg['title'] }}</h1>
<p>{{ $msg['body'] }}</p>
<a href="https://www.teknowize.com/"><button style="background:blue;color:whitesmoke">Learn More</button></a>
<p>Thank you!</p>
</body>
</html>
Step 6- Go to gmail account security then click on the less secure app access setting. If this is On then skip this step otherwise On this setting
Step 7- open testmail.php and pass the message you have in MailController
in mail.blade.php file like below example.
app\Mail\TestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
public $msg;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($msg)
{
$this->msg = $msg;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Test Mail From Teknowize')->view('Email/mail');
}
}
Step 8- Run php artisan serve
Result -