How to Set Up MailHog with Laravel and Sail for Local Email Testing

How to Set Up MailHog with Laravel and Sail for Local Email Testing

When developing a Laravel application, sending and testing emails is often required. MailHog is a simple email-catching service that helps you test outgoing emails locally without actually sending them to real email addresses. In this guide, we will walk through how to set up MailHog with Laravel Sail, Laravel's official Docker environment for local development.

Why MailHog?

MailHog captures all outgoing emails in your application and displays them in a web interface.

Prerequisites

  1. Laravel application set up with Laravel Sail.
  2. Docker and Docker Compose installed.

Step 1: Add MailHog Service to Laravel Sail

  1. Open your docker-compose.yml file
  2. Add MailHog configuration

services:
  # Other services like Laravel, MySQL, etc.
  
  mailhog:
    image: mailhog/mailhog
    ports:
      - "1025:1025"  # SMTP
      - "8025:8025"  # Web interface        

This configuration will start MailHog, exposing SMTP on port 1025 and the web interface on port 8025

Step 2: Update Laravel Environment Configuration

Modify your .env file to use MailHog for email testing. Set the following mail settings:

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

This configuration tells Laravel to use MailHog as the mail server for local email sending.

Step 3: Start Sail Services

Run the following command to start Sail with MailHog:

sail up -d        

This will start all the services defined in your docker-compose.yml file, including MailHog.

Step 4: Test MailHog Setup

Send a test email from your Laravel application.

Mail::to($user)->send(new EmailVerificationToken());        

Step 5: Check the MailHog web interface

Open your browser and navigate to https://localhost:8025. You should see your test email in MailHog's web interface, confirming that MailHog is capturing emails correctly.

Checkout Laravel's and Mailhog site to learn more.

Saulo Rufino de Sá

Analista de Tecnologia da Informa??o (IFRN)

2 个月

?Hi. Solves connection?error?laravel 11: add to mailhog service networks: - sail Thank you

回复

要查看或添加评论,请登录

Bijay Das的更多文章

  • Laravel Applications with Request Context

    Laravel Applications with Request Context

    The request context refers to any data or information specific to a single HTTP request. This data can include details…

  • Advanced S3 File Upload Strategies in Laravel: Beyond the Basics

    Advanced S3 File Upload Strategies in Laravel: Beyond the Basics

    Introduction As seasoned developers with over a decade of experience, we understand that file uploads are more than…

    1 条评论
  • Installing LAMP on an Linux Server

    Installing LAMP on an Linux Server

    Introduction The LAMP stack with Linux, Apache, MySQL, and PHP, is a used web service layer to create and use web apps.…

  • Guide to find command

    Guide to find command

    The command is a cornerstone of any Linux user's toolkit. It allows you to locate files and directories with pinpoint…

  • Guide to Tmux: Productivity with Terminal

    Guide to Tmux: Productivity with Terminal

    Introduction In the world of software development and system administration, productivity tools are essential for…

  • What is an runtime environment?

    What is an runtime environment?

    A runtime environment is a software framework that provides the necessary infrastructure for executing programs or…

  • Mastering JSON.stringify

    Mastering JSON.stringify

    Today, we're diving into the fascinating world of JavaScript and exploring a powerful tool you might already know:…

  • Unleashing the Power of Awk: A Comprehensive Guide to Linux's Command-Line Wizard

    Unleashing the Power of Awk: A Comprehensive Guide to Linux's Command-Line Wizard

    In the vast landscape of Linux command-line utilities, the legendary trio of Alfred Aho, Peter Weinberger, and Brian…

  • A Guide to Setting Up Cron Jobs on Linux

    A Guide to Setting Up Cron Jobs on Linux

    In the rhythmic heartbeat of Linux, where time orchestrates the dance of processes, Cron emerges as the conductor…

  • Organizing route files in Laravel

    Organizing route files in Laravel

    A few days ago I was building an application in Laravel where I faced the problem of having too many routes in my…

社区洞察

其他会员也浏览了