Skip to main content
March 23, 2026Noble Desktop Publishing Team/8 min read

Sending Email: Free PHP & MySQL Tutorial

Master PHP Email Integration with Local Testing Environment

Tutorial Components Overview

MailHog Installation

Local email testing server that catches and displays all outgoing emails without actually sending them to recipients.

Server Configuration

Setting up MAMP Pro and XAMPP to work with MailHog for seamless email testing in development environments.

PHP Implementation

Using PHP's built-in mail() function to send test emails through your configured local development server.

Topics Covered in This PHP & MySQL Tutorial:

Installing MailHog, Setting up MAMP Pro, Setting up XAMPP, Sending a Test Email

Exercise Overview

PHP's built-in email functionality is elegantly simple—just a single function call can dispatch messages to any recipient. However, the real challenge lies in configuring a reliable mail server for your development environment. Without proper testing infrastructure, you're essentially developing blind, unable to verify that your email features work correctly before they reach production.

This exercise walks you through setting up a professional email testing environment using MailHog, a powerful mail catcher that intercepts and displays all outbound emails from your local development server. We'll configure both MAMP Pro and XAMPP to work seamlessly with MailHog, then demonstrate how to send test emails using PHP's mail() function. By the end, you'll have a robust testing setup that captures every email your application sends, allowing you to debug formatting, verify content, and ensure delivery without spamming real inboxes.

Why Use a Mail Catcher

MailHog catches any mail sent to it and displays it locally, preventing test emails from being sent to real recipients during development. This is essential for safe testing environments.

Getting Started with MailHog

MailHog serves as a local SMTP server and web-based email client rolled into one efficient package. Think of it as a black hole for emails—it catches every message sent to it and presents them through a clean, intuitive web interface. This approach eliminates the complexity of configuring actual mail servers while providing complete visibility into your application's email behavior.

For developers working in team environments or attending instructor-led courses, MailHog is typically pre-configured. However, if you're setting up your own development environment, the installation process varies by operating system. The following platform-specific guides will get you up and running quickly.

Installation Methods by Operating System

FeatureMacWindows
PrerequisitesXcode Command Line ToolsNone required
Installation MethodHomebrew package managerDirect executable download
Installation LocationManaged by HomebrewProgram Files/MailHog folder
Recommended: Mac users benefit from automated updates through Homebrew, while Windows users have simpler initial setup.

Installing MailHog on Mac

  1. Open Terminal (found in Applications > Utilities or press Cmd+Space and type "Terminal").

  2. Install the Xcode command line developer tools by typing the following command and hitting Return:

    xcode-select --install
  3. If you get a pop-up asking if you would like to install the tools now, click Install.

  4. Click Agree to the terms of use. The installation typically takes 5-10 minutes depending on your internet connection.

  5. Next, install Homebrew, macOS's premier package manager that simplifies installing development tools like MailHog. In your web browser, navigate to https://brew.sh

  6. Copy the installation command displayed under Install Homebrew.

  7. Paste the command into Terminal and press Return.

  8. Follow the installation prompts. You may need to enter your system password.

  9. If Homebrew is already installed, update it to the latest version by typing:

    brew update
  10. Install MailHog with a single command:

    brew install mailhog

    Homebrew will automatically handle all dependencies and configuration.

Mac Installation Process

1

Install Xcode Tools

Run 'Xcode-select--install' in Terminal and follow prompts to install required development tools

2

Install Homebrew

Visit brew.sh, copy the installation command, paste in Terminal and execute to install the package manager

3

Install MailHog

Execute 'brew install mailhog' command in Terminal to download and install MailHog automatically

Installing MailHog on Windows

  1. Navigate to GitHub.com/mailhog/MailHog in your browser.

  2. Scroll down to the Getting started section.

  3. Click Download the latest release.

  4. Click on the most recent release version. As of 2026, this is typically v1.0.1 or higher.

  5. Under Downloads, select the appropriate version for your system:
    MailHog_windows_amd64.exe for 64-bit systems (most modern computers)
    MailHog_windows_386.exe for 32-bit systems

  6. Create a new folder called MailHog in your Program Files directory and save the executable there. This keeps your system organized and makes the tool easy to locate later.

Windows Installation Process

1

Download from GitHub

Navigate to GitHub.com/mailhog/MailHog and locate the latest release section for download links

2

Select Correct Version

Choose MailHog_windows_amd64.exe for 64-bit systems or MailHog_windows_386.exe for 32-bit systems

3

Install in Program Files

Save the executable file in Program Files within a new folder specifically named MailHog

Running MailHog on Mac

With MailHog installed via Homebrew, launching it becomes a simple terminal command. Windows users can skip to the next section.

  1. Open Terminal if it's not already running.

  2. Start MailHog with the following command:

    mailhog
  3. Press Return. You should see output indicating that MailHog is running and listening on ports 1025 (SMTP) and 8025 (web interface).

  4. Keep Terminal open and running in the background—closing it will stop MailHog.

Running MailHog on Windows

  1. Navigate to Program Files > MailHog and locate the executable file beginning with MailHog.

  2. Double-click the executable and select Run when prompted. This opens MailHog in a Command Prompt window.

  3. If Windows Firewall displays a security alert, click Allow access. MailHog needs network permissions to function as a mail server.

  4. Leave the Command Prompt window open and running. Minimizing is fine, but closing it will terminate MailHog.

Now that MailHog is running, we need to configure your local development environment to route emails through it. The process differs between MAMP Pro and XAMPP, so follow the section that matches your setup.

Windows Firewall Configuration

When running MailHog for the first time, Windows Firewall may block access. Click 'Allow access' when prompted to ensure proper functionality.

Setting up Email: MAMP PRO

MAMP Pro users have the advantage of a streamlined configuration process. Windows users should skip to the XAMPP section below.

  1. Launch MAMP PRO if it's not already running.

  2. To integrate MailHog, we'll modify the php.ini configuration file, which PHP reads during startup. This file controls core PHP behavior, including mail handling.

    Access the file through File > Edit Template > PHP (php.ini) and select the PHP version currently active in MAMP Pro.

    Note: Your active PHP version appears in MAMP Pro's main interface under the PHP Version column.

  3. If MAMP Pro displays a warning about editing system files, click OK to proceed.

  4. We need to configure the sendmail_path directive to route emails through MailHog. For your convenience, open sendmail_path.txt from the phpclass folder in your code editor.

  5. Select and copy the entire line from the text file.

  6. Return to the php.ini file in MAMP Pro.

  7. Locate the existing sendmail configuration around line 570. Look for these lines:

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ;sendmail_path =

    Tip: Use Cmd+F to search for sendmail_path if you can't find it manually.

  8. Replace the commented ;sendmail_path = line with your copied configuration:

    sendmail_path = /usr/local/Cellar/mailhog/1.0.0/bin/MailHog sendmail
  9. The version number in this path must match your installed MailHog version. While 1.0.0 was current when this guide was written, newer versions may be available. To verify your version, paste this path into Finder's address bar:

    /usr/local/Cellar/mailhog/

    The folder listing will show your installed version number.

  10. Update the path if necessary, then save the php.ini file.

  11. Close the file. MAMP Pro will prompt you to restart the servers—click Yes to apply the changes.

MAMP PRO Configuration Steps

1

Edit PHP Configuration

Access File > Edit Template > PHP (php.ini) and select your current PHP version from MAMP Pro

2

Locate sendmail_path

Find the commented sendmail_path line around line 570 using Cmd-F search if necessary

3

Update Path Configuration

Replace the commented line with the correct MailHog sendmail path including version number

4

Restart Servers

Save the php.ini file and restart MAMP Pro servers when prompted to apply changes

Version Number Verification

The sendmail_path must include the correct MailHog version number. You can verify your version by navigating to /usr/local/Cellar/mailhog/ in your browser.

Setting up Email: XAMPP

XAMPP requires configuration of two files to work with MailHog: php.ini for PHP settings and sendmail.ini for SMTP routing. This dual-configuration approach provides more granular control over email handling.

  1. Open the XAMPP Control Panel.

  2. Next to Apache, click the Config button and select PHP (php.ini) from the dropdown menu.

  3. This opens php.ini in your default text editor.

  4. Press Ctrl+F to open the Find dialog.

  5. Search for sendmail and click Find Next.

  6. You'll be taken to a section containing:

    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = postmaster@localhost
  7. Scroll down until you find the XAMPP-specific sendmail configuration:

    ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
    ;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
  8. By default, XAMPP writes emails to disk rather than sending them. To enable actual email transmission through MailHog, modify the configuration as shown:

    ; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    
    ; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
    ;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

    Notice how we've uncommented the sendmail line and commented out the mailtodisk line.

  9. Save and close the php.ini file.

  10. Return to your code editor and open another file.

  11. Navigate to File > Open File.

  12. Browse to the C:\xampp\sendmail folder.

  13. Open sendmail.ini. If you see multiple sendmail files without clear extensions, type sendmail.ini in the filename field and click Open.

  14. Locate the SMTP server configuration around line 14:

    smtp_server=mail.mydomain.com
    
    ; smtp port (normally 25)
    
    smtp_port=25
  15. Modify these settings to point to MailHog:

    smtp_server=localhost
    
    ; smtp port (normally 25)
    
    smtp_port=1025
  16. Save and close the sendmail.ini file.

  17. Return to the XAMPP Control Panel. The changes require an Apache restart to take effect.

    Important: If Apache is installed as a Windows service (indicated by a green checkmark), you may need to restart your entire computer for the changes to apply properly.

  18. Click the Stop button next to Apache.

  19. Click the Start to restart Apache.

    Note: If Apache fails to start or immediately stops after starting, restart your computer, then relaunch XAMPP. The servers should initialize without issues.

XAMPP Configuration Process

1

Configure PHP Settings

Open Apache Config > PHP (php.ini) from XAMPP control panel and locate sendmail configuration section

2

Enable Sendmail Path

Remove semicolon from sendmail_path line and add semicolon to mailtodisk line to switch email handling

3

Edit Sendmail Configuration

Open sendmail.ini from C:/xampp/sendmail folder and configure smtp_server as localhost with port 1025

4

Restart Apache Server

Stop and start Apache server in XAMPP control panel, restart computer if Apache is installed as service

Service Installation Consideration

If Apache is installed as a service (green checkmark visible), you must restart your entire computer for configuration changes to take effect properly.

Sending an Email

With your testing infrastructure in place, you're ready to send emails using PHP's built-in mail() function. This function provides a straightforward interface to your system's mail transport, making email integration remarkably simple.

The mail() function follows this syntax:

mail(to, subject, message, headers, additional_parameters)

While simple in appearance, proper email construction requires attention to headers, encoding, and formatting standards to ensure reliable delivery and professional presentation.

  1. In your code editor, open mail.php from the phpclass folder.

  2. Add the following PHP code at the top of the document:

    <?php 
    mail(
        "youremail@gmail.com", 
        "Hello World", 
        "Hello, this is a test message from my PHP application!", 
        "From: youremail@gmail.com\r\n"
    );
    ?>

    The \r\n sequence after the From address is crucial—it represents a carriage return and line feed required by email protocols. Many mail servers will reject messages that lack proper header formatting.

  3. Save the file, then test it by navigating to the appropriate URL in your browser:

    • Mac (MAMP): localhost:8888/phpclass/mail.php
    • Windows (XAMPP): localhost/phpclass/mail.php

    Monitor your Terminal or Command Prompt window—you should see MailHog processing the email transmission.

  4. To view your captured email, open a browser and navigate to localhost:8025

  5. The MailHog web interface will display, showing an inbox containing your test message. Click on the email to view its contents, headers, and formatting.

  6. Close any open files. Your email testing environment is now fully operational and ready for development work.

You now have a professional-grade email testing setup that will serve you throughout your PHP development journey. MailHog will capture every email your applications send, allowing you to verify functionality, debug issues, and refine your email features without affecting real users or cluttering actual inboxes.

PHP mail() Function Structure

The mail() function follows this format: mail(to, subject, message, headers, additional parameters). The carriage return and line feed (\r\n) after the From address is required by many email servers.

Testing Email Functionality

1

Create PHP Email Script

Add mail() function code to mail.php with recipient, subject, message, and From header properly formatted

2

Execute Email Script

Access the PHP file through your local server (localhost:8888 for Mac or localhost for Windows)

3

Verify Email Delivery

Check Terminal or Command Prompt for email status, then view MailHog inbox at localhost:8025

Key Takeaways

1MailHog provides a safe local email testing environment that catches emails without sending them to real recipients
2Mac installation requires Xcode command line tools and Homebrew package manager for automated setup and updates
3Windows installation involves downloading the appropriate executable file and manually placing it in Program Files
4MAMP PRO configuration requires editing the php.ini file to set the correct sendmail_path with proper MailHog version number
5XAMPP setup involves configuring both php.ini and sendmail.ini files, plus switching from mailtodisk to sendmail functionality
6PHP's mail() function requires specific formatting including recipient, subject, message, headers, and proper carriage return line feeds
7Server restart is essential after configuration changes, with Windows service installations requiring full computer restart
8The MailHog web interface at localhost:8025 displays all captured emails for easy testing and debugging verification

RELATED ARTICLES