PHPMailer.Failed 连接到服务器时出现问题?

Problem with PHPMailer.Failed to connect to server?

我是 PHP 的菜鸟。我连接了 PHPMailer。我正在训练发送电子邮件,但收到错误消息

我的代码

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ("vendor/autoload.php");

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 2;

    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = true;
    $mail->Username = ''; -The mail I'm trying to send an email to
    $mail->Password = ''; - password of this email
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom('', 'admin'); The mail I'm trying to send an email to
    $mail->addAddress('', 'Recipient'); -recipient
    $mail->isHTML(true);
    $mail->Subject = 'Test Mail Subject!';
    $mail->Body    = 'This is SMTP Email Test';

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

我想提前声明一下,这个邮件是不安全的。我也试过改变 STMPSECURE(tls)

我得到了

SMTP ERROR: Failed to connect to server: Connection refused (111)

你必须在SMTP服务器上有一个帐号,例如:smtp.googlemail.com(也是最容易获得的),所以你可以这样设置:

$mail->Host = 'smtp.googlemail.com';
$mail->SMTPAuth = true;
$mail->Username = 'YOUR_GOOGLE_USERNAME';  // NOT email address to send to
$mail->Password = 'YOUR_GOOGLE_PASSWORD';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;