What are common causes of the SMTP Error: Could not connect to SMTP host when sending emails in PHP?

The SMTP Error "Could not connect to SMTP host" typically occurs when there is an issue with the SMTP server configuration or network connectivity problems. To resolve this error, check the SMTP server settings, ensure that the server is reachable from your network, and verify that there are no firewall restrictions blocking the connection.

// Example PHP code snippet to fix SMTP Error: Could not connect to SMTP host
$mail = new PHPMailer(true);

$mail->isSMTP();
$mail->Host = 'smtp.yourmailserver.com';
$mail->Port = 587; // or 465
$mail->SMTPAuth = true;
$mail->Username = 'your-email@example.com';
$mail->Password = 'your-email-password';
$mail->SMTPSecure = 'tls'; // or 'ssl'

// Additional configuration settings if needed

try {
    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}