What are the potential causes of the SMTP error when using PHP mail() function?
The potential causes of the SMTP error when using the PHP mail() function include incorrect SMTP server settings, firewall restrictions blocking outgoing connections, or the SMTP server being down or unreachable. To solve this issue, ensure that the SMTP server settings are correct, check for any firewall restrictions, and verify that the SMTP server is operational.
// Example code snippet to set SMTP server settings for PHP mail() function
ini_set("SMTP","mail.example.com");
ini_set("smtp_port","25");
$headers = "From: sender@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$to = "recipient@example.com";
// Send email using PHP mail() function
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Failed to send email. Please check SMTP settings.";
}