Mailhog is not working in Laravel 8.x + Sail
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
MailHog Fails in Laravel Sail? A Deep Dive into Docker Networking Issues
As a developer leveraging the power of Laravel Sail for rapid application development, we often encounter scenarios where local services, like mail testing tools such as MailHog, fail to integrate correctly within the containerized environment. The issue described—where the configuration seems correct but the web interface (http://localhost:8025) returns a "This page isn’t working" error—is a classic symptom of misconfigured Docker networking or service dependency failures.
This post will walk you through the likely causes of this problem and provide a robust, step-by-step solution to get your MailHog testing fully operational with Laravel Sail.
The Root Cause: Understanding Container Networking
When you use Laravel Sail, everything runs inside Docker containers. Your application code interacts with services (like a mail catcher) via network bridges managed by Docker. The problem usually isn't the environment variables themselves (MAIL_HOST, MAIL_PORT), but rather how those ports are exposed and communicated between the host machine and the internal container network.
In your scenario, setting MAIL_HOST=mailhog and MAIL_PORT=1025 tells Laravel where to send emails (to a service named 'mailhog' on port 1025). However, this configuration alone does not ensure that the MailHog service itself is correctly exposed to the host machine for external access.
The most common failure points are:
- Missing Service Exposure: The MailHog container is running, but its port mapping (the bridge between the container and the host) is incorrect or missing from the configuration setup by Sail.
- Service Startup Order: Other services might be trying to access the port before MailHog has fully initialized and started listening.
- Environment Variable Misinterpretation: The system expecting a specific host name (like
mailhog) might fail if the actual container service name is different, or if the port mapping isn't handled correctly by the underlying Docker setup.
Troubleshooting and the Correct Configuration
To fix this, we need to ensure that MailHog is correctly exposed on your host machine so you can interact with it via a browser. This often involves inspecting how Sail manages custom services or ensuring the necessary ports are published.
Step 1: Verify the Docker Compose Setup (The Core Fix)
While Sail abstracts much of this, understanding the underlying principle is crucial. For services deployed via Docker Compose, networking relies entirely on the ports: directive in your docker-compose.yml file.
If you are using a standard setup, ensure that any custom service or external tool running within the Sail environment has its port explicitly mapped to the host. If MailHog is meant to be accessible externally, it must be published correctly.
Step 2: Checking Environment Variables and Service Status
Double-check your .env file:
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_ENCRYPTION=null
These settings are correct for instructing Laravel to use the MailHog service. The failure is external to this setting, pointing back to the container networking layer.
Step 3: The Solution - Manual Port Mapping or Sail Extension (If Necessary)
If the default Sail setup isn't automatically mapping the mailhog port to localhost:8025, you may need to explicitly define it or ensure that the environment is correctly interpreted by the underlying Docker network.
In many complex setups, especially when dealing with external services not built into the standard Laravel stack, developers sometimes need to extend the Sail configuration or manually inspect the generated Docker Compose file to add a port mapping for the MailHog service.
Best Practice: Always refer back to the official documentation and community examples when setting up infrastructure. For robust container orchestration, understanding how systems like those discussed on laravelcompany.com manage service discovery is invaluable. Ensure that the MailHog container is independently running and listening on the correct internal port (usually 1025), and that the bridge to your host machine is correctly established.
If you are using a custom setup, ensure your Docker Compose file explicitly maps the required ports:
# Example fragment demonstrating the principle of port mapping in Docker Compose
services:
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025" # This maps internal container port 1025 to host port 1025
By confirming that the MailHog service is correctly exposed via ports in the underlying Docker configuration, you resolve the conflict between the application request and the service availability.
Conclusion
The failure you experienced with MailHog in Laravel Sail is almost certainly a networking issue within the Docker environment rather than an issue with your Laravel configuration itself. By shifting focus from the .env file to the underlying Docker Compose structure—specifically ensuring proper port mapping for the MailHog container—you can resolve this problem. Remember, mastering the infrastructure layer (Docker) is just as important as mastering the application code when building robust solutions. Happy coding!