7 min read

A Guide To Upgrade Your Laravel Mail Testing With Mailhog

Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.

Laravel Mail TestingSending emails is an essential feature for most web applications. And Laravel wouldn’t be Laravel if they didn’t turn such a frequently needed feature into a built-in email-sending functionality. In this article, we will guide you through the steps to upgrade Laravel mail testing with Mailhog.

Mailhog is a mail testing tool that allows you to intercept and view email messages sent from your applications. It's particularly handy during development when you need to test your email functionality without sending messages to actual recipients.

So, let's dive in and see how we can test sending emails with Laravel using MailHog on Laravel Sail.

Laravel Mail Testing: Step-by-step Instructions

laravel mail testing

  • Install Laravel Sail

Sail is a lightweight command-line tool that enables interaction with Laravel's Docker environment. You can install Sail by running the following command:

To install Laravel Sail, please follow the official documentation. In short, there are a couple of commands you have to run.

composer require laravel/sail --dev
php artisan sail:install

When prompted for services, you will be asked to specify all the services you need to be included in your docker-compose.yml file; please choose whatever services you think your project might need.

Now that we have Sail configured, we need to set up our mailing service. 

  • Add the following configuration to your docker-compose.yml file under the services section

Open your docker-compose.yml file, which you can find in your Laravel project's root directory. This file contains the configuration for Laravel Sail services.

Add the MailHog service configuration under the services section:


This Laravel mailhog config will create a new MailHog container using the latest available MailHog image. The container's SMTP and web ports (1025 and 8025, respectively) will be mapped to the host machine's ports, allowing you to access the MailHog web interface for viewing sent emails.

Save the docker-compose.yml file.

And now, we need to start our services by running:

./vendor/bin/sail up

or
./vendor/bin/sail up -d # if you want it to run in detached mode
  • To configure your Laravel application to use Laravel MailHog as the mail driver in your local environment, update the following settings in your .env file:

To configure our mail driver to use mailhog on our local environment, we need to configure driver config

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

MAIL_MAILER: Set this value to SMTP to use the Simple Mail Transfer Protocol (SMTP) for sending emails.

MAIL_HOST: Assign the value mailhog, which is the hostname of the MailHog container in Laravel Sail.

MAIL_PORT: Use the default SMTP port for MailHog, which is 1025.

MAIL_USERNAME, MAIL_PASSWORD, and MAIL_ENCRYPTION: Set these values to null because MailHog does not require authentication or encryption.

By updating these settings in your .env file, your Laravel application will send emails through the Laravel MailHog service. This allows you to effectively test and debug your email functionality.

  • In your Laravel application, use the Mail facade to send emails. For example, you can create a new Mailable class using the make:mail Artisan command
php artisan make:mail ContactFormMail --markdown=emails.contact

This will create a new Mailable class in the app/Mail directory and a corresponding markdown template in the resources/views/emails directory.

  • To use Mailhog to send emails in your Laravel application, you can utilize the Mail facade along with a Mailable class. For instance:
use App\Mail\ContactFormMail;
use Illuminate\Support\Facades\Mail;

$email = 'johndoe@example.com';
Mail::to($email)->send(new ContactFormMail($name, $message));

In this example, an email is sent to the specified address using the ContactFormMail Mailable class that you created earlier. The to() method sets the recipient's email address, while the send() method dispatches the email.

And that's it!

Your Laravel application is now configured to send emails with Laravel using MailHog in the Sail environment. To view sent emails, navigate to http://localhost:8025 in your web browser. This allows you to easily test and debug your email functionality during development.

Switching To A Production Mail Service

Once we are ready to deploy our application to production, we can no longer use MailHog as our email service provider in the production environment. So we need to configure some services we will use to send actual emails.

In this example, we will show you how to deploy to production using SendGrid.

SendGrid is a cloud-based email delivery service that helps businesses and developers send transactional and marketing emails with high deliverability rates. It offers SMTP and API integration, analytics, email templates, and a robust infrastructure to effectively manage large volumes of emails. 

When you're ready to move your Laravel application to production, you'll need to switch from MailHog to a production-ready mail service like SendGrid. To do this, follow these steps:

  • Sign up for a SendGrid account 
  • Once you've signed up, navigate to the API Keys section in your SendGrid dashboard: https://app.sendgrid.com/settings/api_keys
  • Click the "Create API Key" button and give it a name. Choose the appropriate access level for your API key, then click "Create & View." Copy the generated API key, as you won't be able to view it again.
  • Update your Laravel application's .env file with the SendGrid settings:
MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@example.com
MAIL_FROM_NAME="${APP_NAME}"

Replace your_sendgrid_api_key with the API key you generated in Step 3. Make sure to also update the MAIL_FROM_ADDRESS and MAIL_FROM_NAME variables with your desired "from" address and name.

  • Deploy your updated Laravel application to your production environment.

Your Laravel application is now configured to send emails using Laravel SendGrid in production. Emails sent from your application will be delivered through SendGrid's reliable and scalable infrastructure, ensuring that your messages reach their intended recipients. 

And that’s all from our guide. So, whenever you need to send an email with Laravel, you can follow the steps outlined in this article, and it will help you do the job seamlessly.

 

Meet the authors

We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.

Contact us