We will learn How to Send Mail in Laravel 8 using Mailtrap
Step 1:
Add Configuration .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=528a733..
MAIL_PASSWORD=73c29..
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 2:
Create Mail
php artisan make:mail MyTestMail
app/Mail/MyTestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyTestMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from rathorji.in')
->view('emails.myTestMail');
}
}
Step 3:
Create Blade View resources/views/emails/myTestMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>{{ $details['title'] }}</h1>
<p>{{ $details['body'] }}</p>
<p>Thank you</p>
</body>
</html>
Step 4:
Add Route routes/web.php
Route::get('send-mail', function () {
$details = [
'title' => 'Mail from rathorji.in',
'body' => 'This is for testing email using smtp'
];
\Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
dd("Email is Sent.");
});
Run Project:
php artisan serve
Open Link:
http://localhost:8000/send-mail
May this help you.