Home > Software design >  PHPMailer Undefined type 'PHPMailer\PHPMailer\PHPMailer'
PHPMailer Undefined type 'PHPMailer\PHPMailer\PHPMailer'

Time:10-21

When i try to type new PHPMailer(true); Then its keep giving me the same error:

Undefined type 'PHPMailer\PHPMailer\PHPMailer'.

Can you tell whats the problem? Thanks!

Code:

<?php
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

require "vendor/autoload.php";

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);

$mail->isSMTP();
$mail->SMTPAuth = true;

$mail->Host = "smtp.rackhost.hu";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Port = 465;

$mail->Username = "[email protected]";
$mail->Password = "Teszt123";

$mail->setFrom("[email protected]", "Djalms");
$mail->addAddress("[email protected]");

$mail->Subject = $subject;
$mail->Body = $message;

$mail->send();

echo "email sent";
?>

The username, Password and the setFrom is empty to keep privacy.

CodePudding user response:

If you've now managed to install PHPMailer with composer (which would explain why you were getting the class not found error), I expect the 504 is caused by this combination of settings:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 465;

That's asking it to do explicit TLS on a port expecting implicit TLS, which will not work, but might take a long time to fail, leading to the 504 error. The difference is explained in the docs, but either of these two combinations should work:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

or

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
  • Related