LARAVEL Tutorial


Laravel 7 Send Email Example Tutorial


In this laravel send mail/email tutorial. You will learn how to configure email smtp detail and send mails/emails.

Laravel has supported many API and drivers like SMTP, SendMail, Mailgun, Sendgrid, Mandrill, Amazon SES, SmartPost, Gmail etc for send mail/email for laravel web applications.

When you are working with Laravel web application. So you have to send mail to users in many ways. Like sometimes the user has to send email verification email. Or send an invoice in the mail. Or other types of information need to be sent in email.

Laravel Send Email/Mail

  • Setup SMTP Credentials in .env
  • Create Controller with Method
  • Create Route & Blade View
  • Email Send and Test

Setup SMTP Credentials in .env

This is the first step, go to .env file and set SMTP details in .env file.

You could do like:

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 

The above email SMTP details are default and necessary. You can also set the following things:

MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=

The above are used by Laravel web applications when you will send emails/mails to users. The from email address and name part of the email that is displayed to users.

Create Controller with Method

This is the second step. in this step, we will create a controller for sending emails/mails.

Just open your command prompt (CMD) and go to your project root directory. Then type the below command for creating a new controller.

php artisan make:controller MailController

After that, You need to create one method in your controller. So go to the => app/Http/Controllers/MailController.php And update the below given code into your controller:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Redirect,Response,DB,Config;
use Mail;
class MailController extends Controller
{
    public function sendEmail()
    {
        $data['title'] = "This is first mail from w3alert1.com";
 
        Mail::send('emails.email', $data, function($message) {
 
            $message->to('w3alrt1@gmail.com', 'Receiver Name')
 
                    ->subject('W3alert.com');
        });
 
        if (Mail::failures()) {
           return response()->Fail('Sorry! Please try again latter');
         }else{
           return response()->success('Great! Successfully send in your mail');
         }
    }
}

Create Route & Blade View

Next step, here you need to create one route and one blade view file.

So go to routes/web.php and update the following route in your file:

Route::get('lara-email-send', 'MailController@sendEmail');

After that, go to your resources/views/ and create a new blade view file name email.blade.php and update the below code into your file:

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

Email Send And Test

Hit the following URL in your browser and test:

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

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

http://localhost/your-project-name/public/lara-email-send