Laravel 9 Mail | Laravel 9 Send Email Example

Laravel 9 Mail - Laravel 9 Send Email Example

Hello friends, Today we will show Laravel 9 send email example. In this example we learn how to send email in laravel 9 application. we will provide to you example of laravel 9 send mail example.

Here, we will show laravel 9 send mail smtp example, how to Send Email in Laravel 9, send mail in Laravel 9 using SMTP, laravel 9 send mail from localhost. In this example we will provide simple example of send email in laravel 9.

Laravel 9 Send Email Example

Step 1: Make Configuration

In the first step we will add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9. Will use those sender configuration for sending email. So you can simply add as like bellow.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 2: Create Mail Class

In this step we will create mail class for send email. Here we will create Demomail class for send mail.

php artisan make:mail DemoMail

app/Mail/DemoMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class DemoMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $mailData;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from Web-tuts.com')
                    ->view('emails.demoMail');
    }
}

Step 3: Create Controller

Now we will create MailController with index() method where we write code for sending mail to given email address. so first let’s create controller by following command and update code on it.

php artisan make:controller MailController

app/Http/Controllers/MailController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
  
class MailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from Web-tuts.com',
            'body' => 'This is for testing email using smtp.'
        ];
         
        Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
           
        dd("Email is sent successfully.");
    }
}

Step 4: Create Routes

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\MailController;
  
  
Route::get('send-mail', [MailController::class, 'index']);

Step 5: Create Blade

In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on “emails” folder.

Read Also :  How to Set Session Data In Laravel ?

resources/views/emails/demoMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Web-tuts.com</title>
</head>
<body>
    <h1>{{ $mailData['title'] }}</h1>
    <p>{{ $mailData['body'] }}</p>
  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
     
    <p>Thank you</p>
</body>
</html>

All the required steps have been done, now you have to type the given following command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/send-mail

I hope you understand of laravel 9 send email and it can help you…

Read Also :  How To Validate Upload File Type Using JavaScript