Skip to content
PHP Logo

How can one troubleshoot SMTP server response errors in PHP mail function?

When troubleshooting SMTP server response errors in the PHP mail function, you can start by checking the error message returned by the server. Common errors include incorrect server settings, authentication issues, or blocked ports. Ensure that your server settings are correct, and that you are using the correct credentials for authentication. You may also need to check if the SMTP port (usually 25) is open and not blocked by your firewall.

php
// Example code snippet to troubleshoot SMTP server response errors in PHP mail function

// Set the SMTP server settings
ini_set("SMTP", "mail.example.com");
ini_set("smtp_port", "25");

// Set the sender and recipient email addresses
$from = "sender@example.com";
$to = "recipient@example.com";

// Set the email subject and message
$subject = "Test Email";
$message = "This is a test email.";

// Send the email
if (mail($to, $subject, $message, "From: $from")) {
    echo "Email sent successfully.";
} else {
    echo "Failed to send email. Please check your server settings.";
}