SMTP 错误:无法连接到服务器:php_network_getaddresses:getaddrinfo 失败:不知道这样的主机 | PHP 邮件程序 6.5

时间:2021-07-05 12:39:56

标签: php email smtp phpmailer

尝试使用 PHPMailer 6.5(当前最新版本)发送邮件。大多数代码是从 PHPMailer 复制粘贴的。所以我希望不需要解释。

错误:

2021-07-05 13:21:33 SMTP ERROR: Failed to connect to server: 

php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message has been sent

代码

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php';

$mail = new PHPMailer();

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;
    $mail->Username   = 'foo@gmail.com';                     //SMTP username
    $mail->Password   = '*******';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;

    $mail->setFrom('foo@gmail.com', 'Megasoft Money');
    $mail->addAddress('foo2@gmail.com');
    $mail->addReplyTo('foo@gmail.com', 'Megasoft Money');

   
    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'PHP Mailer Subject';
    $mail->Body    = 'Hi, this is a test body.';

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

1 个答案:

答案 0 :(得分:0)

您需要限定类名。使用 use 语句:

use PHPMailer\PHPMailer\PHPMailer;

或者只是完全限定它:

$a = new PHPMailer\PHPMailer\PHPMailer();

我建议使用 use 版本,否则您最终将不得不多次编写完全限定的版本。

您还必须使用 SMTP

重复此操作