June 17, 2022

Shubham Patel 0

Step 1 : Make Configuration

In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 8 will use those sender details on email.

So you can simply add as like following in .env file :

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=xyz@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 2 : Add Route

Now create route in your web.php file sending email to the user which you want.

Route::get(‘/email’, [EmailController::class, ‘sendmail’])->name(‘sendmail’);

Step 3 : Create Controller

Php artisan make:controller EmailController

After successfully create new controller you can update below code in your file.

app\http\Controllers\EmailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
    public function sendmail()
    {
        $data = [];
        $data['email'] = 'xyz@gmail.com';
        $data['subject'] = 'hi this is test mail';
        Mail::send('email', $data, function ($message) use ($data) {
            $message->to($data['email'])
                ->subject($data['subject']);
        });
    }
}

Step 4: email

When we send a email then we need to a template which is show the message.

resources\views\email.blade.php

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Welcome!</div>
                <div class="card-body">
                    <h1>data</h1>
                </div>
            </div>
        </div>
    </div>
</div>

Step 5: php artisan serve

Step 6: http://127.0.0.1:8000/email

Thank you for reading…!

Thank you for reading…We hope it helps you out. For any assistance contact us.

Follow us on Linkedin

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *