How to Send Email in Laravel App with Example
Laravel 13-Aug-2020

How to Send Email in Laravel App with Example

In this article , We’ll show you how to send email in laravel with example. We will learn how to send email in laravel using SMTP driver. Laravel provides a API with driver support for SMTP, SendMail,Mailgun, Sendgrid, Mandrill, Amazon SES, SpartPost etc.
Email sending example also work with laravel version 5.8, 5.7 & 5.6

Here, We will give you very simple example of send email in lara application. Benefit of using SMTP server is, you can send email in lara app from your local server also.

Contents

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

Install Laravel 5.7

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

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

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 

Create Route & Blade View

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

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

Next, we need to create email blade view file in views 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 5.7</p>

Create Controller & Method

We need to create new controller EmailController that will manage one method. lets use this below command and create 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('tutsmake@gmail.com', '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');
         }
    }
}

Start 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/SendMail/public/laravel-send-email

If you want to remove public or public/index.php from URL In laravel, Click Me

Conclusion

In this article , We have successfully install new setup and send email using SMTP detail . our examples run quickly.