How to Send Mail in Laravel 8 using Mailtrap?

Share Me
  • 0
  • 0
  • 0
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello Devs,
Hope all are okay.Today, i will show you how to send mail using mailtrap by using laravel 8. Here i will use laravel 8 to send email by using mailtrap.Now in this article; i will implement mailtrap in laravel 8. So in this post I will give you a very simple example of laravel 8 configure mailtrap. So, let’s start and just follow few steps to create a example of laravel 8 mailtrap integration.

Laravel 8 provides us a mail class to send email. We can use several drivers for sending email by using laravel 8. We can use here mailtrap, smtp, Mailgun, Postmark, Amazon SES, and also sendmail. we have to configure on our env file what driver we want to use.

Now in this tutorial, i will give you here step by step instruction how to send email in laravel by using mailtrap. So now we can create blade file design and also with a dynamic information for mail layout. So let’s start and see step by step guide and send email to your requirement.

Step 1: Add Configuration

First of all we need to create account on mailtrap. If you have no account. So now click bellow link to an create account:

Mailtrap Site

After successfully creating an account we will get mail configuration as mail host, mail port, mail username and mail password. 

Now add details from there bellow:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=528a733..
MAIL_PASSWORD=73c29..
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 2: Create Mail

Now in this step i will create my mail class MyFirstMail for sending email. Then we will write some code for which view will call and an object of user. So now let’s run bellow command.

php artisan make:mail MyTestMail

app/Mail/MyFirstMail.php

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyFirstMail 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 Codingspoint.com')
                    ->view('emails.mFirstMail');
    }
}

Step 3: Create Blade View

Now in this step, we need to create a blade view file and write email that we want to send by using mail. Now here we just need to write some dummy text. Create bellow files on “emails” folder.

resources/views/emails/myFirstMail.blade.php

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

Read Also: How to Install Vue JS in Laravel 8 ?

Step 4: Add Route

Now we are at our last step. Now in this step we will create “MyFirstMail” for sending our test email. So now let’s create bellow web route for our testing send our email.

routes/web.php

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

Now we are ready to run and check our example.

It will send your email, let’ see.

Read Also:

Run Project:

php artisan serve

Open Link: How to Convert String Date to Date Format in PHP?

http://localhost:8000/send-mail

Thanks for read. I hope it will help you.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →