App Web Tutorials
  • Home
  • Contact
  • Privacy Policy
Reading: How to send Emails using SMTP in Laravel 9 from localhost and live server
Share
Subscribe Now
App Web TutorialsApp Web Tutorials
Font ResizerAa
Search
  • Home
  • Contact
  • Privacy Policy
Follow US
Copyright © 2017-2025 App Web Tutorials. All Rights Reserved.
Laravel 9Laravel Email TemplatesSMTP Emails

How to send Emails using SMTP in Laravel 9 from localhost and live server

Chetan
By Chetan
Last updated: January 14, 2025
6 Min Read
SHARE

Contents
2. Create a Mail Class3. Create a Mail Controller4. Create an Email Directory and Blade View file template5. Add a Send Email Route in web.php6. Run and Testing the Application

In this tutorial, we will send email in Laravel 9 using SMTP with step by step guide. This method will work with both live and localhost server, because we are using SMTP mail server.

Sending emails is the core functionality of any web application, Laravel 9 also comes with mail drivers to send emails. There are many ways we can send our emails through services like SMTP, Amazon SES, sendmail, Postmark,SendinBlue,Mailjet and Mailgun.

Let’s get started with our tutorial:

Follow the below simple steps to send email in Laravel 9 using SMTP:

  1. Install Laravel 9 & Configure SMTP Credentials in env file
  2. Create a Mail Class
  3. Create a Mail Controller
  4. Create an Email Directory and Blade View file template
  5. Add an Email Route
  6. Run and Test our Application

Step 1: Install Laravel 9 & Configure SMTP Credentials in .env file

First of all, we will install a fresh code of Laravel 9 Framework by running the below command in command prompt or terminal.

You can skip this step if you already have the code setup.

composer create-project --prefer-dist laravel/laravel sendEmail

Once Laravel is installed successfully then configure the .env file with the following SMTP credentials which will be used by Laravel to send email.

For this tutorial we are using Gmail SMTP configuration, you can use any SMTP credentials, make the changes in your .env file as below.

MAIL_MAILER<span style="box-sizing: border-box;">=</span>smtp
MAIL_HOST<span style="box-sizing: border-box;">=</span>smtp<span style="box-sizing: border-box;">.</span>gmail<span style="box-sizing: border-box;">.</span>com
MAIL_PORT<span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box; color: #ae81ff;">465</span>
MAIL_USERNAME<span style="box-sizing: border-box;">=</span>your_email<span style="box-sizing: border-box;">@gmail.com</span>
MAIL_PASSWORD<span style="box-sizing: border-box;">=</span>your_password
MAIL_ENCRYPTION<span style="box-sizing: border-box;">=</span>ssl
MAIL_FROM_ADDRESS<span style="box-sizing: border-box;">=</span>your_email<span style="box-sizing: border-box;">@gmail.com</span>
MAIL_FROM_NAME<span style="box-sizing: border-box;">=</span><span style="box-sizing: border-box; color: #a6e22e;">"${APP_NAME}"</span>

Note: If you are using Gmail to send your email so you will need to enable Less secure app access from your Gmail account setting from following link https://myaccount.google.com/u/1/lesssecureapps .

2. Create a Mail Class

In this step, we will create a Mail class with name TestMail for sending our test emails. This will call the view of test email.

Run the below command to create a Mail class in Laravel project.

php artisan make:mail TestMail

Now update the code on app/Mail/SendMail.php file as below.

<span style="box-sizing: border-box;"><span style="box-sizing: border-box; color: #fd971f; font-weight: bold;"><?php</span>

<span style="box-sizing: border-box; color: #66d9ef;">namespace</span> <span style="box-sizing: border-box;">AppMail</span><span style="box-sizing: border-box;">;</span>

<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateBusQueueable</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateContractsQueueShouldQueue</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateMailMailable</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateQueueSerializesModels</span><span style="box-sizing: border-box;">;</span>

<span style="box-sizing: border-box; color: #66d9ef;">class</span> <span style="box-sizing: border-box; color: #e6db74;">TestMail</span> <span style="box-sizing: border-box; color: #66d9ef;">extends</span> <span style="box-sizing: border-box; color: #e6db74;">Mailable</span>
<span style="box-sizing: border-box;">{</span>
    <span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">Queueable</span><span style="box-sizing: border-box;">,</span> SerializesModels<span style="box-sizing: border-box;">;</span>

    <span style="box-sizing: border-box; color: #66d9ef;">public</span> <span style="box-sizing: border-box;">$mailData</span><span style="box-sizing: border-box;">;</span>

    <span style="box-sizing: border-box; color: #8292a2;">/**
     * Create a new message instance.
     *
     * @return void
     */</span>
    <span style="box-sizing: border-box; color: #66d9ef;">public</span> <span style="box-sizing: border-box; color: #66d9ef;">function</span> <span style="box-sizing: border-box; color: #e6db74;">__construct</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$</span>mailData<span style="box-sizing: border-box;">)</span>
    <span style="box-sizing: border-box;">{</span>
        <span style="box-sizing: border-box;">$this</span><span style="box-sizing: border-box;">-></span><span style="box-sizing: border-box; color: #f92672;">mailData</span> <span style="box-sizing: border-box;">=</span> <span style="box-sizing: border-box;">$</span>mailData<span style="box-sizing: border-box;">;</span>
    <span style="box-sizing: border-box;">}</span>

    <span style="box-sizing: border-box; color: #8292a2;">/**
     * Build the message.
     *
     * @return $this
     */</span>
    <span style="box-sizing: border-box; color: #66d9ef;">public</span> <span style="box-sizing: border-box; color: #66d9ef;">function</span> <span style="box-sizing: border-box; color: #e6db74;">build</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">)</span>
    <span style="box-sizing: border-box;">{</span>
        <span style="box-sizing: border-box; color: #66d9ef;">return</span> <span style="box-sizing: border-box;">$this</span><span style="box-sizing: border-box;">-></span><span style="box-sizing: border-box; color: #e6db74;">subject</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #a6e22e;">'Email From https://www.appwebtut.com/'</span><span style="box-sizing: border-box;">)</span>
                    <span style="box-sizing: border-box;">-></span><span style="box-sizing: border-box; color: #e6db74;">view</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #a6e22e;">'emails.testMailTemplate'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
    <span style="box-sizing: border-box;">}</span>
<span style="box-sizing: border-box;">}</span></span>

3. Create a Mail Controller

In this step, I will create a controller with name EmailController with an index() method that will send an email to your desired email address.

Run the below command to create a mail controller.

php artisan make:controller EmailController

Now update the app/Http/Controllers/EmailController.php code as below.

<span style="box-sizing: border-box;"><span style="box-sizing: border-box; color: #fd971f; font-weight: bold;"><?php</span>

<span style="box-sizing: border-box; color: #66d9ef;">namespace</span> <span style="box-sizing: border-box;">AppHttpControllers</span><span style="box-sizing: border-box;">;</span>

<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateHttpRequest</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">Mail</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">AppMail<span style="box-sizing: border-box;">Test</span>Mail</span><span style="box-sizing: border-box;">;</span>

<span style="box-sizing: border-box; color: #66d9ef;">class</span> <span style="box-sizing: border-box; color: #e6db74;">EmailController</span> <span style="box-sizing: border-box; color: #66d9ef;">extends</span> <span style="box-sizing: border-box; color: #e6db74;">Controller</span>
<span style="box-sizing: border-box;">{</span>
    <span style="box-sizing: border-box; color: #66d9ef;">public</span> <span style="box-sizing: border-box; color: #66d9ef;">function</span> <span style="box-sizing: border-box; color: #e6db74;">index</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">)</span>
    <span style="box-sizing: border-box;">{</span>
        <span style="box-sizing: border-box;">$</span>mailData <span style="box-sizing: border-box;">=</span> <span style="box-sizing: border-box;">[</span>
            <span style="box-sizing: border-box; color: #a6e22e;">'title'</span> <span style="box-sizing: border-box;">=></span> <span style="box-sizing: border-box; color: #a6e22e;">'Test Email From appwebtut.com'</span><span style="box-sizing: border-box;">,</span>
            <span style="box-sizing: border-box; color: #a6e22e;">'body'</span> <span style="box-sizing: border-box;">=></span> <span style="box-sizing: border-box; color: #a6e22e;">'This is the body of test email.'</span>
        <span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;">;</span>

        <span style="box-sizing: border-box; color: #e6db74;">Mail</span><span style="box-sizing: border-box;">::</span><span style="box-sizing: border-box; color: #e6db74;">to</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #a6e22e;">'your_email@gmail.com'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">-></span><span style="box-sizing: border-box; color: #e6db74;">send</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #66d9ef;">new</span> TestMail<span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box;">$mailData</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>

        <span style="box-sizing: border-box; color: #e6db74;">dd</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #a6e22e;">'Success! Email has been sent successfully.'</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span>
    <span style="box-sizing: border-box;">}</span>
<span style="box-sizing: border-box;">}</span></span>

4. Create an Email Directory and Blade View file template

In this step, we will create an email directory in resources/views directory and then create a new email blade template view file with name testMailTemplate.blade.php and paste the below code in it here resources/views/emails/testMailTemplate.blade.php.

<span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">!</span><span style="box-sizing: border-box; color: #f92672;">DOCTYPE</span> html<span style="box-sizing: border-box;">></span>
<span style="box-sizing: border-box;"><</span>html<span style="box-sizing: border-box;">></span>
<span style="box-sizing: border-box;"><</span>head<span style="box-sizing: border-box;">></span>
    <span style="box-sizing: border-box;"><</span>title<span style="box-sizing: border-box;">></span><span style="background-color: #1e1e1e; color: #d4d4d4; font-family: Consolas, 'Courier New', monospace; font-size: 14px; word-spacing: normal;">AppWebTut.com</span><span style="background-color: initial; box-sizing: border-box; font-size: 1em; word-spacing: normal;"><</span><span style="background-color: initial; box-sizing: border-box; font-size: 1em; word-spacing: normal;">/</span><span style="background-color: initial; font-size: 1em; word-spacing: normal;">title</span><span style="background-color: initial; box-sizing: border-box; font-size: 1em; word-spacing: normal;">></span>
<span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">/</span>head<span style="box-sizing: border-box;">></span>
<span style="box-sizing: border-box;"><</span>body<span style="box-sizing: border-box;">></span>
    <span style="box-sizing: border-box;"><</span>h1<span style="box-sizing: border-box;">></span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">{</span> <span style="box-sizing: border-box;">$mailData</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box; color: #a6e22e;">'title'</span><span style="box-sizing: border-box;">]</span> <span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">/</span>h1<span style="box-sizing: border-box;">></span>
    <span style="box-sizing: border-box;"><</span>p<span style="box-sizing: border-box;">></span><span style="box-sizing: border-box;">{</span><span style="box-sizing: border-box;">{</span> <span style="box-sizing: border-box;">$mailData</span><span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box; color: #a6e22e;">'body'</span><span style="box-sizing: border-box;">]</span> <span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;">}</span><span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">/</span>p<span style="box-sizing: border-box;">></span><center>Thankyou!!<span style="background-color: initial; font-size: 1em; word-spacing: normal;"></center></span><span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">/</span>body<span style="box-sizing: border-box;">></span>
<span style="box-sizing: border-box;"><</span><span style="box-sizing: border-box;">/</span>html<span style="box-sizing: border-box;">></span>

5. Add a Send Email Route in web.php

In this step, we will create a web route, add the below code in routes/web.php file to register the route that will send test email.

<span style="box-sizing: border-box;"><span style="box-sizing: border-box; color: #fd971f; font-weight: bold;"><?php</span>

<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">IlluminateSupportFacadesRoute</span><span style="box-sizing: border-box;">;</span>
<span style="box-sizing: border-box; color: #66d9ef;">use</span> <span style="box-sizing: border-box;">AppHttpControllersEmailController</span><span style="box-sizing: border-box;">;</span></span>
/*
|————————————————————————–
| 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!
|
*/
<span style="box-sizing: border-box;">

<span style="box-sizing: border-box; color: #e6db74;">Route</span><span style="box-sizing: border-box;">::</span><span style="box-sizing: border-box; color: #e6db74;">get</span><span style="box-sizing: border-box;">(</span><span style="box-sizing: border-box; color: #a6e22e;">'/send-email'</span><span style="box-sizing: border-box;">,</span> <span style="box-sizing: border-box;">[</span><span style="box-sizing: border-box; color: #e6db74;">EmailController</span><span style="box-sizing: border-box;">::</span><span style="box-sizing: border-box; color: #66d9ef;">class</span><span style="box-sizing: border-box;">,</span> <span style="box-sizing: border-box; color: #a6e22e;">'index'</span><span style="box-sizing: border-box;">]</span><span style="box-sizing: border-box;">)</span><span style="box-sizing: border-box;">;</span></span>

6. Run and Testing the Application

Now, we have completed all required steps to send an email from Laravel 9 using Gmail SMTP server. Now we can start the development server and test the email.

Run the following command to run the development server or open your localhost url in the browser.

php artisan serve

Now go to the browser and hit the below URL to send email from Laravel 9 using SMTP.

http://localhost:8000/send-emailORhttp://localhost/<laravelProjectName<span style="background-color: initial; font-size: 1em; word-spacing: normal;">>/public/send-email</span><laravelProjectName<span style="background-color: initial; font-size: 1em; word-spacing: normal;">> replace this with your directory name</span>

You will get an email like below in your inbox.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article A Beginner’s Guide to Setting Up Visual Studio Code on Your Windows PC Step-by-Step Guide: Easily Set Up Visual Studio Code on Your Windows PC
Next Article Laravel .htaccess www, non-www,http and non-https redirection Master Laravel: The Ultimate Guide to .htaccess Redirects for WWW, Non-WWW, HTTP, and HTTPS
Leave a Comment

Leave a Reply Cancel reply

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

FacebookLike
XFollow
InstagramFollow
Most Popular
How to Install and Use pyenv on Windows, Ubuntu, and macOS: A Complete Guide
The Ultimate Guide to Installing pyenv on Windows, Ubuntu, and macOS
January 21, 2025
How to Host a Python Flask App with SSL on WAMP Server in Windows: A Step-by-Step Guide for Beginners
January 21, 2025
Fixing SSH ‘Unprotected Private Key File’ Error in Windows 11: A Quick Guide
Easily Fix the SSH ‘Unprotected Private Key File’ Error on Windows 11 in Minutes
January 20, 2025
How to Add Eye-Catching Animations to Your HTML Website: A Step-by-Step Guide for Beginners
Power Your HTML Website with Stunning Animations: Easy Guide for Beginners
January 15, 2025
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
Master Git with Ease: The Ultimate Cheatsheet Every Developer Needs
January 21, 2025

You Might Also Like

Laravel .htaccess www, non-www,http and non-https redirection
httpsLaravel 9sslwww

Master Laravel: The Ultimate Guide to .htaccess Redirects for WWW, Non-WWW, HTTP, and HTTPS

2 Min Read
Step-by-step tutorial on how to handle a 404 error and display a custom error page in Laravel
custom pagehandle 404 errorLaravel 9step-by-step tutorial

Step-by-step tutorial on how to handle a 404 error and display a custom error page in Laravel

2 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
App Web Tutorials

We provide tutorials, tips, tricks, and advice for improving programming and development skills.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?