Send Mail In Laravel Using SMTP |Laravel – 8

send_mail_in_laravel

In this blog we will learn about how we can send mail in Laravel using SMTP, how we can configure SMTP in Laravel, I will use mailtrap SMTP to configure the Laravel application, I will put all the SMTP details in the .env file

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

In this blog post I will give you step-by-step instructions to send mail in laravel using SMTP, so please follow each and every steps to achieve your goal.

To send email using SMTP in Laravel we will follow the below steps -:

  • Create an account on mailtrap
  • Download the Laravel Application
  • Configure our .env file
  • Create mail file
  • Create blade file
  • Add route
  • Run laravel development server

Step-1: Visit mailtrap.com and create account

Click on this URL to create a account on mail trap.Once you logged you can see something like image below

mail-trap-dashboard-smtp

Click on messages, when click on message something like below will be open, then on the right side you can see smtp integration, just copy the smtp details, place it in a safe place we will use it later

smtp-integration-image

Step-2: Download the Laravel Application

Open your command prompt under your htdocs and run the below command

composer create-project laravel/laravel laravel-email

Step-3: Configure you .env file for database connection

 MAIL_MAILER=smtp
 MAIL_HOST=smtp.mailtrap.io
 MAIL_PORT=2525
 MAIL_USERNAME=d30db0dafee4d0
 MAIL_PASSWORD=34418fc906b97d
 MAIL_ENCRYPTION=tls

Step -4: Create Mail Class

In this step, we will create mail class TestMail for email sending. Here we will write code for which view will call. So let’s run the below command.

php artisan make:mail TestMail

now go to app/Mail/ and open TestMail.php and then write the below code

<?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;
  
    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 COdehunger.in')
                    ->view('emails.TestMail');
    }
}

Step -5: Create Blade file

Now go to resources/views and create one folder mail and under the mail folder create one file TestMail.blade.php and the write below code in that file

<!DOCTYPE html>
<html>
<head>
    <title>Codehunger.in</title>
</head>
<body>
    <h1>{{ $data['title'] }}</h1>
    <p>{{ $data['body'] }}</p>
    
    <p>Regards, CodeHunger</p>
    <p>Thank you</p>
</body>
</html>

Step-6: Add routes

Go to routes/web.php and add the below code

Route::get('send-mail', function () {
   
    $details = [
        'title' => 'Mail from Codehunger.in',
        'body' => 'Test mail from smtp'
    ];
   
    \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\TestMail($data));
   
    dd("Email has been Sent.");
});

Step-7: Run the Laravel Development Server

To run the development server run the command in your command prompt.

php artisan serve

then open this link http://localhost:8000/send-mail

if you want to more about laravel mail please visit this URL

5 1 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x