How to Send Mail in Laravel 8 using Mailtrap?

How to Send Mail in Laravel 8 using Mailtrap?

I will give you step by step instruction to send email in Laravel using Mailtrap. Laravel 8 provide mail class to send email. You can use several drivers for sending email in Laravel 8: mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail.

Hi All,

Today, i would like to show you laravel 8 send mail using mailtrap. We will use laravel 8 send email mailtrap. In this article, we will implement a laravel 8 mail mailtrap. This post will give you simple example of laravel 8 configure mailtrap. So, let's follow few step to create example of laravel 8 mailtrap integration.

Laravel 8 provide mail class to send 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 env file what driver you want to use.

In this tutorial, i will give you step by step instruction to send email in laravel using mailtrap. you can create blade file design and also with dynamic information for mail layout. so let's see step by step guide and send email to your requirement.

Step 1: Add Configuration

First you need to create account on mailtrap if you don't have. So click bellow link to create account:

Mailtrap Site

After creating account you will get mail configuration as mail host, mail port, mail username, mail passwor. you can see bellow screen shot:

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
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Step 2: Create Mail

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

php artisan make:mail MyTestMail

app/Mail/MyTestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

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 ItSolutionStuff.com')
                    ->view('emails.myTestMail');
    }
}

Step 3: Create Blade View

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

resources/views/emails/myTestMail.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>ItsolutionStuff.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 "MyTestMail" for sending our test email. so let's create bellow web route for testing send email.

routes/web.php

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

    $details = [
        'title' => 'Mail from ItSolutionStuff.com',
        'body' => 'This is for testing email using smtp'
    ];

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

    dd("Email is Sent.");
});

Now you can run and check example.

It will send you email, let' see.

Run Project:

php artisan serve

Open Link:

http://localhost:8000/send-mail

Output:

I hope it can help you...

The Original Article can be found on itsolutionstuff.com

laravel php web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...