search
php

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.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 1 min read
PHP mail Email SMTP Configuration Error Fix

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"
// ✅ 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';
}
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

php

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.

January 8, 2026
php

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.

January 8, 2026
php

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.

January 8, 2026