Laravel Basic | How to Send Mail in Laravel

Raviya Technical
2 min readApr 3, 2022

Do you want to send an email using SMTP in laravel? if yes then I will guide you to laravel send mail example using SMTP driver. I will give you a simple example of how to send mail in laravel using the Mail class. you can also use the Google Gmail driver for sending emails in laravel.

Laravel provides a mail class to send emails. you can use several drivers for sending emails in laravel. you can use SMTP, Mailgun, Postmark, Amazon SES, and send an email. you have to configure on the env file what driver you want to use.

In this tutorial, I will give you step-by-step instructions to send emails in laravel 6. you can create a blade file design and also with dynamic information for mail layout. so let’s see step by step guide and send an email to your requirement.

Step 1: Make Configuration

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, and mail password so laravel will use those sender details on email. So you can simply add as like following.

.env

MAIL_DRIVER=smtpMAIL_HOST=smtp.gmail.comMAIL_PORT=587MAIL_USERNAME=mygoogle@gmail.comMAIL_PASSWORD=rrnnucvnqlbslMAIL_ENCRYPTION=tls

Step 2: Create Mail

In this step, we will create a mail class MyTestMail for email sending. Here we will write code for which view will call and the object of the user. So let’s run the below command.

php artisan make:mail MyTestMail

app/Mail/MyTestMail.php

<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Queue\ShouldQueue;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 raviyatechnical')->view('emails.myTestMail');}}

Step 3: Create Blade View

In this step, we will create a blade view file and write the email that we want to send. now we just write some dummy text. create the below files in the “emails” folder.

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html><html><head><title>raviyatechnical</title></head><body><h1>{{ $details['title'] }}</h1><p>{{ $details['body'] }}</p><p>Thank you</p></body></html>

Step 4: Add Route

Now at last we will create “MyTestMail” for sending our test email. so let’s create the below web route for testing and send an email.

routes/web.php

Route::get('send-mail', function () {$details = ['title' => 'Mail from raviyatechnical','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.");});

Now you can run and check examples.

It will send you an email, let's see.

I hope it can help you…

--

--