Laravel Mail Send | Laravel 8 Send Email using SMTP | Mail Send in Laravel

 

Today i will show you how to send mail using smtp mail port in laravel

 

At first create controller

EmailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
class EmailController extends Controller
{
    public function create()
    {
        return view('email.create');
    }
    public function send(Request $req)
    {
      Mail::send('email.sendmail',[
          'data'=>$req->message
      ],function($message) use ($req){
          $message->to($req->email);
          $message->subject($req->subject);
      });
      return back()->with('success','Mail send done');
    }
}


create a blade file under email folder

create.blade.php

 

<!DOCTYPE html>
<html>
<head>
    <title>send mail</title>
</head>
<body>
<h2>Mail Send Form</h2>
@if(Session::has('success'))
<label>{{Session::get('success')}}</label>
@endif
    <form action="/sendEmail" method="POST">
        {{csrf_field()}}
        <input type="text" name="name" placeholder="name">
        <input type="email" name="email" placeholder="email">
        <input type="text" name="subject" placeholder="subject">
        <textarea  name="message" placeholder="message"></textarea>
        <input type="submit" value="send">
        
    </form>

</body>
</html>

 create mail send file under email folder

sendmail.blade.php 

<h1>Hello</h1>
<p>{{$data}}</p>

Mail configuration 

open .env file and change some line

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

Route

web.php

Route::get('/sendEmail',[App\Http\Controllers\EmailController::class, 'create'] );
 Route::post('/sendEmail',[App\Http\Controllers\EmailController::class, 'send'] );


No comments

Powered by Blogger.