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.
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
| Feature | Mac | Windows |
|---|---|---|
| Prerequisites | Xcode Command Line Tools | None required |
| Installation Method | Homebrew package manager | Direct executable download |
| Installation Location | Managed by Homebrew | Program Files/MailHog folder |
Installing MailHog on Mac
Open Terminal (found in Applications > Utilities or press Cmd+Space and type "Terminal").
Install the Xcode command line developer tools by typing the following command and hitting Return:
xcode-select --installIf you get a pop-up asking if you would like to install the tools now, click Install.
Click Agree to the terms of use. The installation typically takes 5-10 minutes depending on your internet connection.
Next, install Homebrew, macOS's premier package manager that simplifies installing development tools like MailHog. In your web browser, navigate to https://brew.sh
Copy the installation command displayed under Install Homebrew.
Paste the command into Terminal and press Return.
Follow the installation prompts. You may need to enter your system password.
If Homebrew is already installed, update it to the latest version by typing:
brew updateInstall MailHog with a single command:
brew install mailhogHomebrew will automatically handle all dependencies and configuration.
Mac Installation Process
Install Xcode Tools
Run 'Xcode-select--install' in Terminal and follow prompts to install required development tools
Install Homebrew
Visit brew.sh, copy the installation command, paste in Terminal and execute to install the package manager
Install MailHog
Execute 'brew install mailhog' command in Terminal to download and install MailHog automatically
Installing MailHog on Windows
Navigate to GitHub.com/mailhog/MailHog in your browser.
Scroll down to the Getting started section.
Click Download the latest release.
Click on the most recent release version. As of 2026, this is typically v1.0.1 or higher.
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 systemsCreate 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
Download from GitHub
Navigate to GitHub.com/mailhog/MailHog and locate the latest release section for download links
Select Correct Version
Choose MailHog_windows_amd64.exe for 64-bit systems or MailHog_windows_386.exe for 32-bit systems
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.
Open Terminal if it's not already running.
Start MailHog with the following command:
mailhogPress Return. You should see output indicating that MailHog is running and listening on ports 1025 (SMTP) and 8025 (web interface).
Keep Terminal open and running in the background—closing it will stop MailHog.
Running MailHog on Windows
Navigate to Program Files > MailHog and locate the executable file beginning with MailHog.
Double-click the executable and select Run when prompted. This opens MailHog in a Command Prompt window.
If Windows Firewall displays a security alert, click Allow access. MailHog needs network permissions to function as a mail server.
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.
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.
Launch MAMP PRO if it's not already running.
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.
If MAMP Pro displays a warning about editing system files, click OK to proceed.
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.
Select and copy the entire line from the text file.
Return to the php.ini file in MAMP Pro.
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.
Replace the commented ;sendmail_path = line with your copied configuration:
sendmail_path = /usr/local/Cellar/mailhog/1.0.0/bin/MailHog sendmailThe 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.
Update the path if necessary, then save the php.ini file.
Close the file. MAMP Pro will prompt you to restart the servers—click Yes to apply the changes.
MAMP PRO Configuration Steps
Edit PHP Configuration
Access File > Edit Template > PHP (php.ini) and select your current PHP version from MAMP Pro
Locate sendmail_path
Find the commented sendmail_path line around line 570 using Cmd-F search if necessary
Update Path Configuration
Replace the commented line with the correct MailHog sendmail path including version number
Restart Servers
Save the php.ini file and restart MAMP Pro servers when prompted to apply changes
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.
Open the XAMPP Control Panel.
Next to Apache, click the Config button and select PHP (php.ini) from the dropdown menu.
This opens php.ini in your default text editor.
Press Ctrl+F to open the Find dialog.
Search for sendmail and click Find Next.
You'll be taken to a section containing:
; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = postmaster@localhostScroll 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"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.
Save and close the php.ini file.
Return to your code editor and open another file.
Navigate to File > Open File.
Browse to the C:\xampp\sendmail folder.
Open sendmail.ini. If you see multiple sendmail files without clear extensions, type sendmail.ini in the filename field and click Open.
Locate the SMTP server configuration around line 14:
smtp_server=mail.mydomain.com ; smtp port (normally 25) smtp_port=25Modify these settings to point to MailHog:
smtp_server=localhost ; smtp port (normally 25) smtp_port=1025Save and close the sendmail.ini file.
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.
Click the Stop button next to Apache.
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
Configure PHP Settings
Open Apache Config > PHP (php.ini) from XAMPP control panel and locate sendmail configuration section
Enable Sendmail Path
Remove semicolon from sendmail_path line and add semicolon to mailtodisk line to switch email handling
Edit Sendmail Configuration
Open sendmail.ini from C:/xampp/sendmail folder and configure smtp_server as localhost with port 1025
Restart Apache Server
Stop and start Apache server in XAMPP control panel, restart computer if Apache is installed as service
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.
In your code editor, open mail.php from the phpclass folder.
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.
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.
To view your captured email, open a browser and navigate to localhost:8025
The MailHog web interface will display, showing an inbox containing your test message. Click on the email to view its contents, headers, and formatting.
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.
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
Create PHP Email Script
Add mail() function code to mail.php with recipient, subject, message, and From header properly formatted
Execute Email Script
Access the PHP file through your local server (localhost:8888 for Mac or localhost for Windows)
Verify Email Delivery
Check Terminal or Command Prompt for email status, then view MailHog inbox at localhost:8025