Watch Youtube Video (HINDI) :
Welcome to our comprehensive tutorial on sending emails using PHPMailer in the Laravel framework. In this video, we'll walk you through the entire process, step by step, so you can easily integrate email functionality into your Laravel applications.
Send an email using PHPMailer in this post we will see a practical YouTube video example. Sending mail in laravel little bit different from laravel providing default simple mail sending service. In this post content, we will send emails with the help of PHPMailer. So if you have a question in your mind How to Send Mail using PHPMailer in Laravel? This Laravel PHPMailer Step-by-Step Tutorial will help you.
PHPMailer is a popular open-source PHP library that enables developers to send email messages from a PHP application. It provides a convenient way to send emails with attachments, HTML content, and various other features. PHPMailer simplifies the process of sending email through SMTP, as well as using other mail transfer protocols like Sendmail and Mail.
Here are the steps need to be followed for integration into your laravel app :
- Step 1. Create Laravel App
- Step 2. Composer Install PHPMailer
- Step 3. Mail Configuration
- Step 4. Create A Controller
- Step 5. Define Routes
- Step 6. Blade File
- Step 7. Start Development Server & Test
Step 1. Create Laravel App
We will create a new fresh laravel app, using the below command. This command will install a new fresh closed laravel application clone.
composer create-project laravel/laravel example-app
Step 2. Composer Install PHPMailer
Install PHPMailer composer, Use the below given command. This command will install the PHPMailer library in your application. This will help you send emails using access directly PHPMailer class.
composer require phpmailer/phpmailer
Step 3. Mail Configuration
Open your .env file. If the file is not available just copy the ".env.example" content and paste in the ".env" file. Here you have to mail configuration.
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=<YOUR EMAIL ADDRESS> MAIL_PASSWORD=<YOUR EMAIL PASSWORD> MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}"
Step 4. Create A Controller
We will create a "SendMailController", Use the below given command in your app terminal. This command will create a controller. For this control, we will make use of sent mail using PHPMailer.
php artisan make:controller SendMailControllerapp/Http/Controllers/SendMailController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class SendMailController extends Controller { /** * Write code on Method * * @return response() */ public function index(Request $request) { return view('sendemail'); } /** * Write code on Method * * @return response() */ public function store(Request $request) { $mail = new PHPMailer(true); try { /* Email SMTP Settings */ $mail->SMTPDebug = 0; $mail->isSMTP(); $mail->Host = env('MAIL_HOST'); $mail->SMTPAuth = true; $mail->Username = env('MAIL_USERNAME'); $mail->Password = env('MAIL_PASSWORD'); $mail->SMTPSecure = env('MAIL_ENCRYPTION'); $mail->Port = env('MAIL_PORT'); $mail->setFrom(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME')); $mail->addAddress($request->email); $mail->isHTML(true); $mail->Subject = $request->subject; $mail->Body = $request->body; if( !$mail->send() ) { return back()->with("error", "Email not sent.")->withErrors($mail->ErrorInfo); } else { return back()->with("success", "Email has been sent."); } } catch (Exception $e) { return back()->with('error','Message could not be sent.'); } } }
Step 5. Define Routes
Create Routes for the send mail. Just copy the below route and paste it into your web.php file. Make sure that you call the controller at the top of the web route file.
routes/web.php<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\SendMailController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('send-php-mailer',[SendMailController::class, 'index'])->name('send.php.mailer'); Route::post('send-php-mailer-submit',[SendMailController::class, 'store'])->name('send.php.mailer.submit');
Step 6. Blade File
Here is the demo mail sent html blade file code sample. Copy this HTML code and paste your created "sendemail.blade.php" file.
resources/views/sendemail.blade.php<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Email Send Using PHPMailer - webappfix.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css"> </head> <body> <div class="container mt-5" style="max-width: 750px"> <h1>Email Send Using PHPMailer - webappfix.com</h1> @if ($message = Session::get('success')) <div class="alert alert-success alert-dismissible"> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('error')) <div class="alert alert-danger alert-dismissible"> <strong>{{ $message }}</strong> </div> @endif <form method="post" action="{{ route('send.php.mailer.submit') }}" enctype="multipart/form-data"> @csrf <div class="form-group"> <label>Recipient Email:</label> <input type="email" name="email" class="form-control" /> </div> <div class="form-group"> <label>Email Subject:</label> <input type="text" name="subject" class="form-control" /> </div> <div class="form-group"> <label>Email Body:</label> <textarea class="form-control" name="body"></textarea> </div> <div class="form-group mt-3 mb-3"> <button type="submit" class="btn btn-success btn-block">Send Email</button> </div> </form> </div> </body> </html>
Step 7. Start Development Server
Start development server. Use the below-given command in your app terminal. This command will start the local development server.
php artisan serve
Copy the below-given URL below in your browser. You will see an email send form. Fill in all the details and click on this send button.
http://localhost:8000/send-email
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Sendgrid Email Send Tutorial Laravel/PHP/cURL Example
- Laravel 10 Firebase Web Push Notification Tutorial
- Select2 ajax example
- How To Integrate Stripe Payment Gateway in Laravel 9
- Casting Attribute in Laravel Eloquent Models
- Create Custom Route File With New Service Provider Laravel 8 9
- Error : Array to string conversion Laravel
- File name required when file upload laravel validation rules
- Different Solutions For Fix 419 Page Expired laravel
- Queue Laravel Command Tutorial Example