How can one troubleshoot SMTP connection issues in PHPMailer, such as the "unable to connect to smtp.domain.de" error?
To troubleshoot SMTP connection issues in PHPMailer, such as the "unable to connect to smtp.domain.de" error, you can check the SMTP server settings, ensure the correct port is being used, verify the authentication credentials, and confirm that the server is reachable from your network.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.domain.de';
$mail->Port = 587; // or 465
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email.';
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}