How To Send Email In Laravel 9 Using Mailtrap

Websolutionstuff | Jul-27-2022 | Categories : Laravel

In this article, we will explore the process of sending emails in Laravel 9 using Mailtrap. We will delve into how Laravel 9 and Laravel 10 can be employed to send emails with the assistance of Mailtrap, a valuable service designed for secure email testing in development and staging environments.

Mailtrap acts as a virtual inbox, capturing your emails before they reach real users. This allows you to thoroughly test and fine-tune your email campaigns, ensuring their effectiveness and correctness.

Let's proceed to learn how to send emails through Mailtrap in Laravel 8, Laravel 9 and Laravel 10, leveraging this powerful tool to optimize your email functionality.

Step 1: Create a Mailtrap Account

Step 2: Setup .env file Configuration

Step 3: Create Mailable Class

Step 4: Create Blade File

Step 5: Create Route

 

Step 1: Create a Mailtrap Account

In this step, we will create a mailtrap account and create a testing inbox. So, open mailtrap.io and create an account.

After creating an account we need MAIL_USERNAME and MAIL_PASSWORD.

 

 

Step 2: Setup .env file Configuration

Now, we will set up mail configuration in the .env file.

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

 

Step 3: Create Mailable Class

In this step, we will create a mailable class using the laravel artisan command.

php artisan make:mail TestMail

This command will create a new file app/Mail/TestEmail.php.

<? php

namespace App\Mail;
   
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
  
class TestMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;

    }

    public function build(){

        return $this->subject('This is Testing Mail')
                    ->view('emails.test');
    }
}

 

 

Step 4: Create Blade File

Let's create a file under app/resources/views/emails/test.blade.php and add this code.

<!DOCTYPE html>
<html>
<head>
    <title>How To Send Email In Laravel 9 Using Mailtrap - Websolutionstuff</title>
</head>
<body>
    <h5>{{ $user['name'] }}</h5>
    <p>{{ $user['info'] }}</p>
    <p>Thank you</p>
</body>
</html> 

 

Step 5: Create Route

In this step, we will create a route for sending emails.

routes/web.php

Route::get('send-mail', function () {

    $user = [
        'name' => 'Websolutionstuff',
        'info' => 'This is mailstrap example in laravel 9'
    ];

    \Mail::to('[email protected]')->send(new \App\Mail\TestMail($user));

    dd("Successfully send mail..!!");

});

 

Conclusion:

In conclusion, we've covered two essential topics in Laravel, focusing on version 9 and 10. In Laravel 9, we've learned how to send emails efficiently using Mailtrap, a service that enhances email testing and debugging during application development.

 


You might also like:

Recommended Post
Featured Post
Laravel 10 Send Bulk Mail Using Queue
Laravel 10 Send Bulk Mail Usin...

In this article, we will see laravel 10 send bulk mail using a queue. Here, we will learn about how to send bulk ma...

Read More

Mar-13-2023

How To Add Bootstrap 5 Modal Popup In Laravel 9
How To Add Bootstrap 5 Modal P...

In this article, we will see how to add bootstrap 5 modal popup in laravel 9. We will learn how to use the bootstrap 5 m...

Read More

Nov-02-2022

CRUD With Image Upload In Laravel 10 Example
CRUD With Image Upload In Lara...

In this article, we will see crud with image upload in laravel 10 examples. Here, we will learn how to image upload with...

Read More

Mar-27-2023

How To Install PHP CURL Extension In Ubuntu
How To Install PHP CURL Extens...

In this article, we will focus on the installation process of the PHP cURL extension in Ubuntu 22.04. The PHP cURL exten...

Read More

Jul-21-2023