Raviya Technical
Published in

Raviya Technical

How to Send Mail in Laravel?

Do you want to send an email using SMTP in laravel? if yes then I will guide you to laravel 6 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 google Gmail driver for sending emails in laravel.

Laravel provides a mail class to send an email. you can use several drivers for sending emails in laravel. you can use SMTP, Mailgun, Postmark, Amazon SES, and Sendmail. 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. you can create 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, mail password so laravel 6 will use those sender details on email. So you can simply add as like following.

Location:- .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=test
MAIL_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 object of user. So let’s run the below command.

php artisan make:mail TestMail

Location:- app/Mail/TestMail.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 rajtechnologies.com')->view('emails.TestMail');}}

Step 3: Create Blade View

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

Location:- resources/views/emails/TestMail.blade.php

<!DOCTYPE html><html><head><title>rajtechnologies.com</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 “TestMail” for sending our test email. so let’s create below web route for testing send an email.

Location:- routes/web.php

Route::get('send-mail', function () {$details = ['title' => 'Mail from ItSolutionStuff.com','body' => 'This is for testing email using smtp'];\Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\TestMail($details));dd("Email is Sent.");});

Now you can run and check examples.

It will send you an email, let’ see.

I hope it can help you…

Use For Testing Mail STMP on Local Server

https://mailtrap.io

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store