← Other topics

Local email testing in Laravel

Video Notes

When running a Laravel application on a production server, you’ll want it to be configured to use a real world mail server (e.g. MailGun, SendGrid) so that any outgoing emails will end up in your user’s inboxes.

During testing and development, though, you’ll want to “catch” any outgoing emails so that you can confirm everything is working as expected without actually sending real emails.

There are several ways to do this including:

  1. Route all outgoing mail to a log file
  2. Use a local SMTP server like MailHog
  3. Use a development-specific SMTP server provided by a web service like MailTrap
  4. Route all outgoing mail to global to address

This guide will cover details for setting up each of the above options.

Example set up

To begin, we need some code in our application that will send an email. For this simple example, we can set up a route (/test-email) that invoke’s the Mail facade’s raw method:

# file: /routes/web.php
use Illuminate\Support\Facades\Mail;

Route::get('/test-email', function () {
    Mail::raw('This is a test...', function ($message) {
        $message->to('janedoe@gmail.com')->subject('Testing 123...');
    });

    dd('Email sent!');
});

When this route is invoked, it should trigger an email to janedoe@gmail.com. Of course, the goal is to not actually send an email to janedoe@gmail.com but instead capture that email so we can confirm it matches our expectations. Knowing this - before we trigger this route, let’s configure our mail settings...

Log

To capture any outgoing emails in your log file, open your the .env file in the root of your application and set MAIL_MAILER to log.

Once this is done, trigger the above route (/test-email) and then open /storage/logs/laravel.log and you should see the contents of your email:

MailTrap

MailTrap is a web service that provides a dummy inbox for capturing outgoing emails. Create a free account at Mailtrap.io.

Within your account, open the Demo inbox and under SMTP Settings > Integrations choose Laravel and it will give you the environment settings you can copy into your .env file:

Example settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-mailtrap-smtp-username
MAIL_PASSWORD=your-mailtrap-smtp-password
MAIL_ENCRYPTION=tls

MailHog - Local SMTP server

Like MailTrap, Mailhog provides a dummy inbox for capturing outgoing emails. The difference, however, is you run Mailhog directly on your computer - it’s not an external web service.

If you installed Laravel via Composer you should update your .env file to use these mail settings:

MAIL_MAILER=smtp
MAIL_HOST=0.0.0.0:1025
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

To start the Mailhog server for a Composer-built Laravel application, run the command mailhog.

If you’re running Laravel via Sail/Docker you should update your .env file to use these mail settings:

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

When using Laravel Sail, Mailhog is automatically running by default, so no additional commands are needed.

Regardless of whether you’re running Laravel via Composer or Sail, you can access the Mailhog interface via http://localhost:8025.

Global to address

Finally, you can inject some code into your AppServiceProvider that will make it so that any outgoing email is routed to a specific email address when your application is running in a local/development environment:

# /app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Mail;

public function boot()
{
    if ($this->app->environment('local')) {
        Mail::alwaysTo('mail@codewithsusan.com');
    }
}

Unlock all the notes for $4

No subscriptions, no auto-renewals.

Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.

Unlocking gets you access to the notes for this video plus all 200+ guides on this site.

Your support is appreciated. Thank you!

Payment Info

/
$4 6 months
$25 forever
Please check the form for errors
Questions? help@codewithsusan.com
← Other topics