Laravel Email Send

version : Laravel 10
Post Time : 09-06-2024

Laravel Email Send

install laravel
         - composer create-project --prefer-dist laravel/laravel:^10.0 laravel_demo

Create DemoController Controller  :
           - php artisan make:controller DemoController

app/Http/Controllers/DemoController.php

                                      
                                        

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Exception;
class DemoController extends Controller
{
   public function index(Request $request)
   {
       return view('mail_demo');
   }
   public function sendMail(Request $request)
   {
       $validatedData = $request->validate([
           'email' => 'required|email',
       ]);

       $mailData['email'] = $request->input('email');
       try{
           $mail = Mail::send('mails.demo_mail', $mailData , function($message) use ($mailData) {
               $message->to($mailData['email'])
                       ->subject('TechTutorialstuff Demo Mail');
           });
           return redirect()->route('mailDemo')->with('success', 'Successfully send...!!');

       }catch (Exception $e){
           return redirect()->route('mailDemo')->with('error', 'Something went wrong...!!');
       }
   }
}

Create mail_demo blade file :-
Resources/views/mail_demo.blade.php

                                      
                                        

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
       <!-- Bootstrap CSS -->
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> 
   <style>
       .error{
           color:red;
       }
       .basic-form{
           display:flex;
           justify-content:center;
       }
   </style>
</head>
<body>
   <div class="mb-3 row text-center">
       <h1>Mail Demo</h1>
   </div>
   <div class="row basic-form">
       <div class="col-md-6">
           <form class="dz-form pb-3" action="{{ route('sendMail') }}" method="POST" name="mail_form" id="mail_form">
               @csrf
               @if(Session::has('error'))
                   <div class="alert alert-danger" id="success-alert">
                       {{Session::get('error')}}
                   </div>
               @endif
               @if(Session::has('success'))
                   <div class="alert alert-success" id="success-alert">
                       {{Session::get('success')}}
                   </div>
               @endif
               <h3 class="form-title m-t0">Email</h3>
               <div class="dz-separator-outer m-b5">
                   <div class="dz-separator bg-primary style-liner"></div>
               </div>
               <div class="form-group mb-3">
                   <input type="text" class="form-control" placeholder="Email" name="email" value="{{ old('email') }}">
                   @error('email')
                       <label class="error">{{ $message }}</label>
                   @enderror
               </div>
               <div class="form-group text-center mb-5 forget-main">
                   <button type="submit" class="btn btn-primary">Save</button>
               </div>
           </form>
       </div>
   </div>
</body>
</html>

Create mails/demo_mail blade file :-
Resources/views/mails/demo_mail.blade.php

                                      
                                        

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>portal mail layout</title>
   <link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@600&family=Montserrat:ital,wght@1,600&family=Mulish:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;0,1000;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900;1,1000&family=Roboto+Serif:opsz,wght@8..144,700&display=swap" rel="stylesheet">
   <style type="text/css">
       a:hover {text-decoration: none !important;}
   </style>
</head>
<body marginheight="0" topmargin="0" marginwidth="0" style="margin: 0px;" leftmargin="0">
   <table cellspacing="0" border="0" cellpadding="0" width="100%" bgcolor="#fff" style="height: 100vh; font-weight: 500; font-family: 'Mulish',Arial;">
       <tr>
           <td>
               <table style="max-width:100%;  margin:0 auto;" width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                   <tr>
                       <td>
                           <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" style="max-width:700px; background-color: #f1f0f5;">
                               <tr style="display: block; padding: 20px 0 20px 28px;">
                                   <td>
                                       <a href="javascript:void(0);" style="display: flex; align-items: center; text-decoration: none;">
                                           <img src="{{asset('admin/images/tts.png')}}" alt="Header Logo" style="width: 35px; height: 35px;">
                                           <span style="cursor: pointer; font-weight: 600; margin-left: 4px; color: #000;">TechTutorialStuff</span>
                                       </a>
                                   </td>
                               </tr>
                               <tr>
                                   <td style="padding: 0 28px;">
                                       <h1 style="font-weight:700;font-size:28px;font-style:normal;color:#666666;margin-bottom:16px; text-align: center; margin: 35px 0;">Demo Mail !</h1>
                                       <p style="font-weight:700;font-size:28px;font-style:normal;color:#666666;margin-bottom:16px; text-align: left;"> Hello, </p>
                                       <p style="line-height: 1.5; margin-bottom: 20px;">Name : {{ $email }}</p>
                                       <p style="line-height: 1.5;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p>
                                   </td>
                               </tr>
                               <tr style="display: block; padding:0 28px;">
                                   <td style="padding-bottom: 10px;">
                                       <p style="font-weight: 700; color: #666666;">Best regards,</p>
                                       <p>The TechTutorialStuff Services Team</p>
                                   </td>
                               </tr>
                           </table>
                       </td>
                   </tr>
               </table>
           </td>
       </tr>
   </table>
</body>
</html>

routes/web.php

                                      
                                        

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
   return view('welcome');
});
Route::get('/mail_demo', [DemoController::class,'index'])->name('mailDemo');
Route::post('/send_mail', [DemoController::class, 'sendMail'])->name('sendMail');

Set mail configuration setting in .env file
Set your email account and password and other configuration.

                                      
                                        

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
mail_username=demo@gmail.com 
MAIL_PASSWORD=password    
MAIL_ENCRYPTION=tls
mail_from_address="demo@gmail.com"
MAIL_FROM_NAME="${APP_NAME}"