What is the common SMTP error when sending mails through localhost using PHP?
The common SMTP error when sending mails through localhost using PHP is "Failed to connect to server: Connection refused (111)". This error occurs when the SMTP server is not properly configured or not running on the localhost. To solve this issue, ensure that an SMTP server is installed and running on your localhost, and configure your PHP script to connect to the correct SMTP server.
<?php
// Set the SMTP server configuration
ini_set('SMTP', 'localhost');
ini_set('smtp_port', 25);
// Send mail using localhost SMTP server
$to = "recipient@example.com";
$subject = "Test Mail";
$message = "This is a test email.";
// Send mail
if (mail($to, $subject, $message)) {
echo "Mail sent successfully";
} else {
echo "Failed to send mail";
}
?>