The error mail() has been disabled for security reasons occurs when the PHP mail() function is disabled in the server configuration. Hosting providers often disable it to prevent spam abuse or require an authenticated mail system (SMTP).
This guide will help you diagnose and fix the PHP mail() function disabled error, whether you're on a VPS, dedicated server, or shared hosting environment.
Run the following command to check the disabled PHP functions:
Expected output (if mail() is disabled):
exec,passthru,shell_exec,proc_open,system,mail
If mail appears in the list, PHP is blocking it.
Run:
Example output:
Loaded Configuration File: /etc/php/8.1/cli/php.ini
Your php.ini file may be in /etc/php/8.1/apache2/php.ini (for Apache) or /etc/php/8.1/fpm/php.ini (for Nginx).
Open the correct php.ini file using a text editor:
Find this line:
disable_functions = exec,passthru,shell_exec,proc_open,system,mail
Remove mail from the list:
disable_functions = exec,passthru,shell_exec,proc_open,system
Save the file (CTRL + X, then Y, then Enter).
For Apache:
For Nginx with PHP-FPM:
Run:
Expected output (if mail() is enabled):
exec,passthru,shell_exec,proc_open,system
If mail() is no longer listed, it is enabled!
1. Install PHPMailer:
2. Use the following SMTP PHP script:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com';
$mail->Password = 'your-password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('your-email@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email.';
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
| Issue | Solution |
|---|---|
| mail() still not working | Ensure you edited the correct php.ini file (cli vs apache2 vs fpm). |
| Shared hosting doesn't allow mail() | Use SMTP (PHPMailer) instead. |
| Mail server not sending emails | Check if Postfix or Exim is installed and running. |
| Task | Command/Action |
|---|---|
| Check if mail() is disabled | php -r "echo ini_get('disable_functions');" |
| Find php.ini File | php --ini |
| Enable mail() in php.ini | Remove mail from disable_functions |
| Restart Server | sudo systemctl restart apache2 or php-fpm |
| Verify Fix | Run php -r "echo ini_get('disable_functions');" |
| Alternative (If mail() is blocked) | Use PHPMailer with SMTP |