Fix PHP mail()
Function Not Working in XAMPP (Windows, macOS, Linux/Ubuntu)
Is the PHP mail function not working in XAMPP? Learn how to send emails using mail()
by configuring sendmail
or external SMTP on Windows, macOS, and Linux/Ubuntu.
🧩 Introduction
By default, the mail()
function in PHP doesn’t work out of the box in XAMPP because it’s not connected to any mail transfer agent (MTA). This issue is especially noticeable on Windows, where no built-in mail server exists.
In this blog, we’ll configure mail functionality using sendmail and SMTP across Windows, macOS, and Linux/Ubuntu environments.
❗ Why mail()
Fails
sendmail
not configured- SMTP settings missing or incorrect
- Firewall blocking mail ports (587/465/25)
- Antivirus/email clients blocking outgoing mail
- No MTA on local environment
🖥️ Fix on Windows (Using SMTP via sendmail)
✅ Step-by-Step:
- Locate
php.ini
:- XAMPP Control Panel > Apache > Config >
php.ini
- XAMPP Control Panel > Apache > Config >
- Enable mail logging (optional but helpful):
log_errors = On error_log = "C:\xampp\php\logs\php_error_log"
- Edit
php.ini
:
Find and edit:[mail function] SMTP=smtp.gmail.com smtp_port=587 sendmail_from = your_email@gmail.com sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
- Edit
sendmail.ini
:
Open:C:\xampp\sendmail\sendmail.ini
Replace content with:smtp_server=smtp.gmail.com smtp_port=587 smtp_ssl=auto auth_username=your_email@gmail.com auth_password=your_app_password
⚠️ Note: Use an App Password if 2FA is enabled. - Restart Apache.
- Test PHP Script:
<?php if(mail("to@example.com", "Test Subject", "Test Body", "From: your_email@gmail.com")) { echo "Mail sent successfully!"; } else { echo "Mail sending failed!"; } ?>
🍏 Fix on macOS
- Install Sendmail (optional):
On macOS, use postfix or tools likemsmtp
. - Use PHP + SMTP:
- Install
msmtp
:brew install msmtp
- Install
- Configure PHP to use SMTP:
In yourphp.ini
(/Applications/XAMPP/xamppfiles/etc/php.ini
):sendmail_path = "/usr/local/bin/msmtp -t"
- Configure
~/.msmtprc
:defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt account gmail host smtp.gmail.com port 587 from your_email@gmail.com user your_email@gmail.com password your_app_password account default : gmail
- Set permission:
chmod 600 ~/.msmtprc
- Restart Apache and test mail.
🐧 Fix on Linux/Ubuntu
- Install
msmtp
:sudo apt update && sudo apt install msmtp
- Configure PHP:
In/opt/lampp/etc/php.ini
, add:sendmail_path = "/usr/bin/msmtp -t"
- Configure msmtp (user-level):
# ~/.msmtprc defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt account gmail host smtp.gmail.com port 587 from your_email@gmail.com user your_email@gmail.com password your_app_password account default : gmail
- Permission Fix:
chmod 600 ~/.msmtprc
- Restart Apache:
sudo /opt/lampp/lampp restartapache
- Test PHP mail code as mentioned above.
💡 Alternative: Use PHPMailer for Reliability
Using libraries like PHPMailer is recommended for production as it handles SMTP authentication, attachments, and error reporting more effectively.
📌 Conclusion
PHP mail()
not working in XAMPP is a configuration issue, not a code issue. Whether you’re on Windows, macOS, or Linux, following these steps will allow your local server to send emails using Gmail SMTP or other providers.
💬 Facing Errors?
Drop your configuration and error message in the comments, and I’ll help you troubleshoot it personally.