Laravel send email has become an important part when you use laravel web applications. send mail is best way to interact with users, when they register, when verifying registrations, and when resetting passwords.
Sometime local server may not send emails when using the PHP mail() , or it may make it quite difficult to do.
So in this blog, we will learn how to Laravel send email from localhost using a Gmail SMTP server.
In this example, we will create a simple html form with name, email and phone
- Requirement
- Install Laravel
- Install Laravel UI
- Install Node js Packages
- Add the Gmail SMTP server configuration
- Steps to generate gmail app password
- Generate a Mailable
- Write Email messages
- Set up the routes
- Set up the controller
- Set Up View File
- Conclusion
Requirement
To follow this blog you will need the following:
Composer installed globally
Git already installed
Node js already installed
Laravel 8
A Gmail account
PHP 8.1 already installed
Install Laravel
Open Composer type below command
composer create-project laravel/laravel send-mail
After install go into folder
cd send-mail
Install Laravel UI
composer require laravel/ui
Below command will install bootstrap in your Laravel.
php artisan ui bootstrap
Install Node js Packages
npm install
Next, we have to run the below command for asset compilation:
npm run dev
Next, run below command
npm run build
Add the Gmail SMTP server configuration
Open .env, and update the following variables:
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=<Enter your Gmail address> MAIL_PASSWORD=<Enter your Gmail app password> MAIL_ENCRYPTION=TLS MAIL_FROM_ADDRESS=<Enter from email address>
Steps to generate gmail app password
Login into your gmail account
Go to link
https://myaccount.google.com/security?pli=1
Click on 2-Step Verification
Next click on “Get started” button
Next Enter Phone and Click on “NEXT” button
Enter Otp and Click “TURN ON” button
Next click below link
https://myaccount.google.com/apppasswords
Next Copy password and paste in MAIL_PASSWORD field
MAIL_PASSWORD=<Enter your Gmail app password>
Generate a Mailable
When building Laravel applications, every type of email sent by your application is represented as a “mailable” class. These classes are stored in the app/Mail directory.
“mailable” will be generated when you create your first mailable class using the make:mail Artisan command:
run below command to generate mailable class
php artisan make:mail SendMail
open file “send-mail\app\Mail\SendMail.php”
paste below code in “SendMail.php” file
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; class SendMail extends Mailable { use Queueable, SerializesModels; public $mailData; /** * Create a new message instance. */ public function __construct($mailData) { $this->mailData = $mailData; } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( subject: 'Local Email Send', ); } /** * Get the message content definition. */ public function content(): Content { return new Content( view: 'mail.send-mail', ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { return []; } }
Write Email messages
Create the Blade file in “send-mail/resources/views/mail/send-mail.blade.php”
add below code in file “send-mail.blade.php”
<!DOCTYPE html> <html> <head> <title>webdav.in</title> </head> <body> <h2>Welcome</h2> <p>Hi, {{$mailData['name']}}</p> <p><b>Successfully Receive Mail From LocalHost</b></p> <p>Your Email: {{$mailData['email']}}</p> <p>Your Mobile No.: {{$mailData['mobile']}}</p> <p>Thank you for connect</p> </body> </html>
Set up the routes
Go to folder send-mail\routes\web.php
open “web.php” file
paste below code in “web.php” file
<?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 and all of them will | be assigned to the "web" middleware group. Make something great! | */ /* Display Form route* */ Route::get('/',function() { return view('form'); }); /* Mail Send Route */ Route::post('send-mail', [SendMailController::class, 'SendMailFunction'])->name('send.mail.form');
Set up the controller
Next we need to create a controller
run below command
php artisan make:controller SendMailController
add below code in send-mail\app\Http\Controllers\SendMailController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use App\Mail\SendMail; class SendMailController extends Controller { public function SendMailFunction(Request $request) { $mailData = [ 'name' =>$request->firstName, 'mobile' =>$request->mobileNumber, 'email' => $request->emailID ]; Mail::to($request->emailID)->send(new SendMail($mailData)); return redirect()->back()->with(['success' => 'Email Send Successfully']); } }
Set Up View File
create file “send-mail\resources\views\form.blade.php”
Add below code in “form.blade.php”
<!doctype html> <html> <head> @vite(['resources/sass/app.scss', 'resources/js/app.js']) </head> <body> <div class="container mt-5 mb-5 d-flex justify-content-center"> <div class="card px-1 py-4"> <div class="card-body"> @if(Session::has('success')) <div class="alert alert-success"> {{Session::get('success')}} </div> @endif <h1 class="card-title mb-3">Newsletter Form</h1> <h6 class="information mt-4">Please provide following information</h6> <form method="POST" action="{{route('send.mail.form')}}"> <input type="hidden" name="_token" value="{{ csrf_token() }}" /> <div class="row"> <div class="col-sm-12"> <div class="form-group"> <!-- <label for="name">Name</label> --> <input class="form-control" type="text" name='firstName' placeholder="Name" required> </div> </div> </div> <br> <div class="row"> <div class="col-sm-12"> <div class="form-group"> <div class="input-group"> <input class="form-control" type="text" name='mobileNumber' placeholder="Mobile" required> </div> </div> </div> </div> <br> <div class="row"> <div class="col-sm-12"> <div class="form-group"> <div class="input-group"> <input class="form-control" name='emailID' type="text" placeholder="Email ID" required> </div> </div> </div> </div> <br> <button class="btn btn-primary btn-block confirm-button" type="submit">Submit</button> </form> </div> </div> </div> </body> </html>
You can see mail in Gmail Account
Conclusion
In this blog, we learned how to send emails in Laravel from localhost. Laravel Mail is a broad concept on its own but this blog can present as a great starter guide.
Suggested post : How to send email from wordpress website