In this tutorial you will learn about the Laravel 7/6 Send Email Tutorial Example and its application with practical example.
In this Laravel 7/6 Send Email Tutorial I will show you how to send email in laravel. In this tutorial you will learn how to send email in laravel using SMTP driver. Laravel comes with built-in api and driver support for SMTP, SendMail, Mailgun, Sendgrid, Mandrill, Amazon SES, SpartPost etc. In this article I will share example of sending an email in laravel application.
Laravel 7/6 Send Email Tutorial Example
In this step by step tutorial you will understand how to send emails in Laravel using SMTP driver.
- Install Laravel Setup
- Configuration SMTP in .env
- Create Route & Blade View
- Create Controller & Method
- Run Development Server
1. Install Laravel Fresh Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
2. Configuration SMTP in .env
In this step we will configure smtp settings in .env file as following:
1 2 3 4 5 6 |
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 |
3. Create Route & Blade View
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get('laravel-send-email', 'EmailController@sendEmail'); |
Next, we will create 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:
1 2 |
<h1>{{ $title }}</h1> <p>This is my first Email using Laravel Application</p> |
4. Create Controller & Method
Now, lets create a controller named EmailController using command given below –
1 |
php artisan make:controller EmailController |
Now open the controller let’s go to app/Http/Controllers/EmailController.php. Put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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"; Mail::send('emails.email', $data, function($message) { $message->to('yoursite@gmail.com', 'Receiver Name') ->subject('Test 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
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/laravel-send-email |