Laravel 7/6 Send Email Tutorial

Laravel 7/6 Send Email Tutorial

In this article, You will learn, how to send email in laravel with example. We will learn how to send email in laravel using SMTP driver. Laravel provides an API with driver support for SMTP, SendMail, Mailgun, Sendgrid, Mandrill, Amazon SES, SpartPost etc.

Here, We will give you a very simple example of sending an email in the Lara application. The benefit of using the SMTP server is, you can send an email in the Lara app from your local server also.

Laravel Send Email Example Tutorial

Follow the following steps to send email using laravel 7/6 applications:

  • Install Laravel Setup
  • Configuration SMTP in .env
  • Create Route & Blade View
  • Create Controller & Method
  • Run Development Server

1. Install Laravel Fresh Setup

First We need to Download fresh Laravel setup. Use the below command to download the Laravel Application fresh setup on your system.

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

2. Configuration SMTP in .env

In this step, we will set SMTP credential in .env file. Let’s open .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=Add your user name here
MAIL_PASSWORD=Add your password here
MAIL_ENCRYPTION=tls

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending the emails.

3. Create Route & Blade View

We need to create a route, open your routes/web.php update the below route :

 Route::get('laravel-send-email', 'EmailController@sendEmail');

Next, we need to create an email blade view file in view folder of view. Go to the resources/views/ and create a view file name email.blade.php. Put bellow code:

<h1>{{ $title }}</h1>
<p>This is my first Email using Laravel Application</p>

4. Create Controller & Method

We need to create a new controller EmailController that will manage one method. lets use this below command and create a Controller.

php artisan make:controller EmailController

Now open the controller let’s go to the => app/Http/Controllers/EmailController.php. Put the below Code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Redirect,Response,DB,Config;
use Mail;
class EmailController extends Controller
{
    public function sendEmail()
    {
        $data['title'] = "This is Test Mail Tuts Make";

        Mail::send('emails.email', $data, function($message) {

            $message->to('[email protected]', 'Receiver Name')

                    ->subject('Tuts Make Mail');
        });

        if (Mail::failures()) {
           return response()->Fail('Sorry! Please try again latter');
         }else{
           return response()->success('Great! Successfully send in your mail');
         }
    }
}

5. Run Development Server

In this step, we will use the php artisan serve command. It will start your server locally

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/laravel-send-email

If you are not run PHP artisan server command, direct go to your browser and type the URL

http://localhost/Blog/public/laravel-send-email

Conclusion

In this article, We have successfully installed a new setup and send emails using SMTP detail. our examples run quickly.

You may like

  1. Laravel Send Email Made Easy Using Mailable Class Example
  2. Laravel Tutorial From Scratch | Step By Step

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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