No articles found
Try different keywords or browse our categories
Fix: PHP mail() not working error
Quick fix for PHP mail() not working error. Learn how to configure PHP mail settings and use alternatives like SMTP for reliable email delivery.
The ‘PHP mail() not working’ error occurs when the PHP mail() function fails to send emails due to misconfigured mail settings, missing mail server, or security restrictions.
How the Error Happens
❌ Error Scenario:
// ❌ This may fail silently
$result = mail('user@example.com', 'Subject', 'Message');
// Returns false or appears to work but no email received
✅ Quick Fix - Configure Email Delivery
Solution 1: Configure php.ini
; ✅ Update php.ini for mail settings
[mail function]
SMTP = smtp.gmail.com
smtp_port = 587
sendmail_from = your-email@gmail.com
sendmail_path = "/usr/sbin/sendmail -t -i"
Solution 2: Use PHPMailer (Recommended)
// ✅ Install PHPMailer via Composer
// composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com';
$mail->Password = 'your-app-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('your-email@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Test Subject';
$mail->Body = 'Test Message';
$mail->send();
Solution 3: Check Mail Logs
// ✅ Enable error reporting for mail
if (!mail($to, $subject, $message, $headers)) {
error_log('Mail function failed');
echo 'Mail failed to send';
} else {
echo 'Mail sent successfully';
} Related Articles
Fix: Session_start(): Cannot start session error
Quick fix for 'Session_start(): Cannot start session' error in PHP. Learn how to properly configure and manage PHP sessions.
Fix: Undefined constant error PHP fix
Quick fix for 'Undefined constant' error in PHP. Learn how to properly define and use constants in PHP applications.
Fix: Upload_max_filesize exceeded error PHP fix
Quick fix for 'Upload_max_filesize exceeded' error in PHP. Learn how to increase file upload limits for your PHP applications.