Laravel Mail - Send Email Example In Laravel 8

In this post, I will give you a demo laravel mail - send an email example in laravel 8. Here I will show you how to send mail in laravel 8, email is a very basic and most important feature in the web development field and it is necessary for all clients to send and receive information and important data.

Laravel 8 provides several ways to send an email. You can also use the PHP method for sending mail and you can also use some email service providers such as mail, Gmail, SMTP, mailgun, etc. So, you can choose anyone and set the configuration for sending an email. Laravel 8 provides a Mail facade for mail send that has several methods for sending an email.

So, in this tutorial, I will give you information about sending emails using SMTP in laravel 8.

So, let's see laravel 8 send email example.

Step 1: Set Configuration

In this step we will set configuration in .env file for sending mail, here we are using SMTP mailtrape so set according to port also as below.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=TLS

 

 

Step 2 : Create Mail Using Artisan Command

Laravel provides a built-in mail class for sending mail. So, we need to create testmail class for the same,

php artisan make:mail TestMail

Now you will find the file in this location app/Mail/TestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail 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('mail.Test_mail');
    }
}

 

 

Step 3 : Create Blade File

Now, we will create the Test_mail.blade.php file in this path view/mail/Test_mail.blade.php

In this file, we need to add some dummy text for email test purposes.

Hi <br/>
This is Test Mail From Techsolutionstuff.<br />
Thank you !!

 

Step 4 : Create Route

Now, Create a route for testing send mail in laravel 8.

Route::get('test_mail','App\Http\Controllers\UserController@testMail');

 

Step 5 : Create Controller

 Now create UserController and add the below code.

public function testMail()
{
	$mail = 'your_email_id@gmail.com';
	Mail::to($mail)->send(new TestMail);

	dd('Mail Send Successfully !!');
}

And after that, you will get output like the below screenshot.

send_email_example_in_laravel_8

 


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS