In this tutorial you will learn about the Laravel 8 Send Email Tutorial and its application with practical example.
In this Laravel 8 Send Email Tutorial I will show you how to send mail using gmail smtp server. In this tutorial you will learn to send email using gmail smtp server in laravel application. In this article I will share example to send email in laravel 8 using mailable and mailtrap. We will use Laravel 8 builtin mailable markdown class to send emails. In order to send email using mailtrap we will implement laravel 8 builtin mailable markdown class. In this post I’ll show you how to integrate mailtrap to send email in laravel 8 application.
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 lara8blog |
Setup Database Credentials
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=lara8blog DB_USERNAME=root DB_PASSWORD= |
Make Email Configuration
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.
1 2 3 4 5 6 7 8 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=mygoogle@gmail.com MAIL_PASSWORD=rrnnucvnqlbsl MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=mygoogle@gmail.com MAIL_FROM_NAME="${APP_NAME}" |
Create Mail
Now, lets create a new laravel mailable class using following artisan command:
1 |
php artisan make:mail MyTestMail |
Once the above command executed, it will create a new laravel mailable class MyTestMail.php in app/Mail/ directory. Open the MyTestMail.php file and put the following code in it.
app/Mail/MyTestMail.php
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 |
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class MyTestMail extends Mailable { use Queueable, SerializesModels; public $details; /** * Create a new message instance. * * @return void */ public function __construct($details) { $this->details = $details; } /** * Build the message. * * @return $this */ public function build() { return $this->subject('Mail from w3adda.com') ->view('emails.myTestMail'); } } |
Create Blade View
In this step, we will create view/blade file to setup email template. Open resources/views/emails/myTestMail.blade.php file, put the following code.
resources/views/emails/myTestMail.blade.php
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html> <head> <title>w3adda.com</title> </head> <body> <h1>{{ $details['title'] }}</h1> <p>{{ $details['body'] }}</p> <p>Thank you</p> </body> </html> |
Add Route
Now, 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 2 3 4 5 6 7 8 9 |
Route::get('send-mail', function () { $details = [ 'title' => 'Test Mail', 'body' => 'This is for testing email using smtp' ]; \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details)); dd("Email is Sent."); }); |
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/send-mail |