In this tutorial you will learn about the Laravel 8 Send Mail using Gmail SMTP Server and its application with practical example.
In this Laravel 8 Send Mail using Gmail SMTP Server 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.
Step 1: Make Email Configuration
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}" |
Step 2: 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'); } } |
Step 3: 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> |
Step 4: 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 |