DEV Community

Carlos Santos
Carlos Santos

Posted on

Laravel Mailtrap Setup

Laravel Mailtrap Setup

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 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.
 

Step 1: Mailtrap Account

  • Go to https://mailtrap.io/ and sign up.
     

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

Mailtrap Inboxes

  • 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.
     

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

Mailtrap Inbox Settings

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

Mailtrap Laravel Env

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"

Enter fullscreen mode Exit fullscreen mode

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');
    }
}

Enter fullscreen mode Exit fullscreen mode

resources\views\emails\test.blade.php (Markdown file)

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

The cake is a lie

Thanks,<br>
{{ config('app.name') }}
@endcomponent
Enter fullscreen mode Exit fullscreen mode

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());
});

...
...
Enter fullscreen mode Exit fullscreen mode

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

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/

I hope this article was helpful.

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

Top comments (0)