In this tutorial you will learn about the Laravel 7/6 Send Email Using Mailable Class Tutorial and its application with practical example.
In this Laravel 7 send email example using mailable tutorial I’ll show you how to send email in laravel using a mailable class with example. In this step by step tutorial I’ll guide you through 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. In this tutorial we will use simple example of mailable class to send email in laravel application.
Laravel Send Email Using Mailable Class Example
- Install laravel Fresh Setup
- Configuration SMTP in .env
- Mailable Class
- Create Route & Blade View
- Create Controller & Method
- Run Development Server
Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel larablog |
Configure Database In .env file
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=larablog DB_USERNAME=root DB_PASSWORD= |
Configuration SMTP in .env
Now we will configure SMTP credentials in the .env file. Let’s open .env file.
.env
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 |
Mailable Class
Now, create a mailable class for sending emails, use the following command :
1 |
php artisan make:mail MailNotify |
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 28 29 30 31 32 33 34 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class MailNotify extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. * * @return void */ public function __construct() { // } /** * Build the message. * * @return $this */ public function build() { return $this->view('view.name'); //here is your blade view name } } |
Create Route & Blade View
Now, we need to create a route to send email, open your routes/web.php input the below route :
1 |
Route::get('laravel-send-email', 'EmailController@sendEmail'); |
Next, we will create an email blade view file in views folder of view. Go to the resources/views/emails and create a view file name email.blade.php. Put bellow code:
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>Laravel Send Email Example</title> </head> <body> <h1>This is test mail</h1> <p>Thank you, {{ $user->name }}</p> </body> </html> |
Create Controller & Method
In this step we will create a new controller EmailController with one method to send email. Open your terminal and run following command to create controller:
1 |
php artisan make:controller EmailController |
Now open the controller file and 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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Redirect,Response,DB,Config; use Mail; class EmailController extends Controller { public function sendEmail() { $user = auth()->user(); Mail::to($user)->send(new MailNotify($user)); if (Mail::failures()) { return response()->Fail('Sorry! Please try again latter'); }else{ return response()->success('Great! Successfully send in your mail'); } } } |
Run Development Server
In this step, we will start the development server using following command:
1 |
php artisan serve |
Now, visit the following link to send email:
1 |
http://localhost:8000/laravel-send-email |