Hello Devs,

In this tutorial, we are going to learn how to send email in laravel 7.

Follow this step by step guide given below:




Step 1 : 


Install Laravel 7

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


.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourgoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls



Step 2: 


Create Mail

php artisan make:mail MyDemoMail


app/Mail/MyDemoMail.php

<?php
  
namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class MyDemoMail 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('myDemoMail');
    }
}



Step 3: 


Create Blade View resources/views/myDemoMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Rathorji.in Mail Demo</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\MyDemoMail($details));
   
    dd("Email is Sent.");
});


Run this command:

php artisan serve

Open this URL:

http://localhost:8000/send-mail


I hope this example helps you.