Local Email Testing in Laravel – The Full Guide

January 24th, 2025 • 5 minutes read time

Almost every Laravel application sends emails, so you'll need a way to receive emails locally to ensure they look good.

In this article, I've compiled a list of almost every method of local email testing in Laravel, with instructions on how to get set up. Whatever method you choose, any of these solutions will allow you to preview what your users will eventually see.

This is my favourite method when I'm quickly prototyping or getting started on a project, because it doesn't involve setting up any external software/services to be able to view Laravel email being sent.

To log email when it's sent, change the MAIL_MAILER value in your .env file to log:

MAIL_MAILER=log

Once you send an email, it'll appear in your laravel.log file and look something like this:

[2025-01-23 18:58:45] local.DEBUG: From: Laravel <hello@example.com>
To: alex@codecourse.com
Subject: Welcome
MIME-Version: 1.0
Date: Thu, 23 Jan 2025 18:58:45 +0000
Message-ID: <0785ddf1d2146e1d94375e7350cbb314@example.com>
Content-Type: multipart/alternative; boundary=NvLIT37A

--NvLIT37A
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Laravel: http://laravel-local-email-testing.test

# Introduction

The body of your message.

Button Text:

Thanks,
Laravel

© 2025 Laravel. All rights reserved.

I've truncated the rest, but you get the idea.

Of course, the downside to this approach is that you can't actually see the contents of the email, but it's great for quickly testing.

Probably the most common way to receive locally sent email in Laravel applications is to use a web-based or OS-based piece of software that acts as a mail server. This allows you to receive email in a similar environment you'd expect your users to.

Let's run through setting up both Mailpit and Helo. There are other options, but these are the most widely used within the Laravel community.

To install Mailpit, you can either do so via package managers (like Homebrew for MacOS), install via a bash script, or download the static binaries.

In my case, I'll install it through brew:

brew install mailpit

Consult the installation page for Mailpit for other installation methods and operating systems.

Once you're done, you should be able to run mailpit (depending on how you've installed it) to start a server.

> mailpit

INFO[2025/01/23 19:11:14] [smtpd] starting on 0.0.0.0:1025
INFO[2025/01/23 19:11:14] [http] starting server on http://localhost:8025/

This will start a server accessible at http://localhost:8025/, which looks like a mailbox!

In my case, I just need to update my MAIL_PORT to 1025, and I can send email directly to the Mailpit server. Here's my full MAIL_ configuration for clarity:

MAIL_MAILER=smtp
MAIL_SCHEME=null
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME="Laravel Local Email Testing"
MAIL_PASSWORD=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

And here's what the email looks like once sent:

blog/4kinWIY5WLX7Dt2uOFXYQYZpeUooLiibO7607czv.webp

Helo is very similar but is a paid application by Beyond Code. The benefit of Helo is that you can install it natively on your machine rather than have it open as a web page.

Once you've purchased and installed Helo (pretty easily by just downloading it), you can configure your Laravel application to send email there, much like Mailpit:

MAIL_MAILER=smtp
MAIL_SCHEME=null
MAIL_HOST=127.0.0.1
MAIL_PORT=2526
MAIL_USERNAME="Helo inbox testing"
MAIL_PASSWORD=null

Once we send an email, we will see our email appear in Helo's interface!

blog/XNZaUSv5vfywOs9yne6s606IVrBenXruKu2pGj53.webp

There are platforms similar to Mailpit and Helo, like Mailtrap. These operate in almost exactly the same way but don't live on your local machine. Instead, you're sending your local emails to a 3rd-party platform.

Services like Mailtrap usually offer more features than you'd find in local, free alternatives. You can also continue to integrate the rest of your email-sending strategy with one of these platforms.

We won't specifically go through the steps to set up Mailtrap, but services like this exist if you prefer them.

The laravel-mail-preview package alerts you within your application when an email is sent. You can then click to preview it — pretty neat.

Let's set it up!

composer require spatie/laravel-mail-preview

Next, under config/mail.php, swap over the smtp mailer transport value with an env value:

'mailers' => [
    'smtp' => [
        'transport' => env('MAIL_TRANSPORT'),
        'scheme' => env('MAIL_SCHEME'),

Then add this to .env, setting the value to preview:

MAIL_TRANSPORT=preview

Now, register the middleware in bootstrap/app.php:

use Spatie\MailPreview\Http\Middleware\AddMailPreviewOverlayToResponse;

->withMiddleware(function (Middleware $middleware) {
    $middleware->append([
        AddMailPreviewOverlayToResponse::class
    ]);
})

And then register the routes for mail preview in routes/web.php:

Route::mailPreview();

When you send an email, you'll see a little dialogue that can also preview the recently sent email.

blog/xToNq4q83W79yHpMab6HzPfebilHeio7vOvmZNLg.webp

While this could interfere with your application's interface or simply not work if you're using something like Inertia, it's a great option if you're sending and tweaking many emails and need to constantly preview/adjust them.

Similar to the Spatie laravel-mail-preview approach, where we can click emails and view them in the browser, we can do this manually without any packages required.

To do this, just return your Mailable either from a route closure or controller:

Route::get('/email', function () {
    return new Welcome();
});

If you open up /email in your browser, you'll see the fully rendered email!

This works because the base Mailable class for emails in Laravel implements the Renderable interface, which allows the object to be returned as a response and rendered with the render method implementation.

This turns your Mailable classes into things you can render to the browser — a super easy way to preview them as you build them!

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!