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.
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.
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
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
When running MailHog for the first time, Windows Firewall may block access. Click 'Allow access' when prompted to ensure proper functionality.
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.
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.
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
Key Takeaways