Laravel Mailtrap Setup
Tags: laravel, mailtrap, php, webdev
Published: 2021-12-18 21:57:19


![Laravel Mailtrap Setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2207hov30vj3u417628.png)
## Introduction

How will this email look? Will it look nice on mobile? Is the email data correct? You can test all that and more with Mailtrap.

[Mailtrap](https://mailtrap.io/) is a testing tool designed to trap any emails coming out of your application. Setting up Mailtrap is a must for your development environment, this way you can test your application functionalities without worrying about spamming real users.
&nbsp;
## Step 1: Mailtrap Account

- Go to [https://mailtrap.io/](https://mailtrap.io/) and sign up.
&nbsp;

- Once you are logged in you will see a menu with 3 options; Inboxes, API, and Billing. Go to Inboxes.

![Mailtrap Inboxes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z2dyms6uj5ixc9x98fq.png)

- On the Inboxes page you will see a list of your inboxes and you will also have the option of creating a new project or creating a new inbox. We will use the default inbox for this setup.
&nbsp;

- Click on the inbox and on SMTP Settings select Integrations -> Laravel.

![Mailtrap Inbox Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etv476mwsy7rx3c9x25z.png)

- Here you will see the .env variables you need to copy.

![Mailtrap Laravel Env](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5h0vacnpoyd34ykitli.png)

## Step 2: Laravel Configuration
Add (or replace) mail environment variables from Mailtrap to your application .env file.

```
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=YOUR_INBOX_USERNAME
MAIL_PASSWORD=YOUR_INBOX_PASSWORD
MAIL_ENCRYPTION=tls

# Also
MAIL_FROM_ADDRESS=no-reply@test.local
MAIL_FROM_NAME="Mailtrap Test App"

```

## Step 3: Test
**Create simple test mailable with markdown.**
`php artisan make:mail MailtrapTest --markdown=emails.test`

_App\Mail\MailtrapTest.php_

```
<?php

namespace App\Mail;

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

class MailtrapTest extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.test');
    }
}

```

_resources\views\emails\test.blade.php (Markdown file)_

```
@component('mail::message')
# TEST EMAIL

The cake is a lie

Thanks,<br>
{{ config('app.name') }}
@endcomponent
```

**Add test route**

```
...
...

Route::get('/mailtrap/test', function () {
    // Using a temp email. 
    // This way we can test that emails are really trapped.
    $testEmail = 'rognodugni@vusra.com';

    return Mail::to($testEmail)->send(new MailtrapTest());
});

...
...
```

Now hit test URL. You should have a new email on your Mailtrap inbox and you should not have any email from your application on the test email inbox.

![Mailtrap Inbox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5xq8bjk5qr48v4alfal.png)

## Conclusion
Mailtrap is a very useful tool for development and it's super easy to setup with Laravel. You can find more information at [https://mailtrap.io/blog/send-email-in-laravel/](https://mailtrap.io/blog/send-email-in-laravel/)

I hope this article was helpful.

If you have any feedback or found mistakes, please don’t hesitate to reach out to me.