PHPMailer 错误:php_network_getaddresses:getaddrinfo 失败:LOCALHOST(XAMPP) 中的名称解析暂时失败

时间:2021-06-06 09:53:37

标签: php email xampp smtp phpmailer

首先,我在 Mac OS 和最新的 PHP 邮件程序上使用 XAMPP,我知道这可能与 DNS 问题有关,但请阅读。

这是完整的错误:

2021-06-06 09:04:03 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()

2021-06-06 09:04:23 Connection failed. Error #2: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution [/opt/lampp/htdocs/enfu2020/123abc/srcs/PHPMailer/src/SMTP.php line 388]

2021-06-06 09:04:23 Connection failed. Error #2: stream_socket_client(): Unable to connect to smtp.gmail.com:587 (php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution) [/opt/lampp/htdocs/enfu2020/123abc/srcs/PHPMailer/src/SMTP.php line 388]

2021-06-06 09:04:23 SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (0)

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

代码如下:

try {

$mail = new PHPMailer(true);

//Server settings
$mail->isSMTP();                                            //Send using SMTP
$mail->Host = 'smtp.gmail.com';                     //Set the SMTP server to send through
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;                                   //Enable SMTP authentication
$mail->Username = 'anymail@gmail.com';                 // SMTP username
$mail->Password = 'anyPassword';                                //SMTP password
   $mail->SMTPDebug = 3;


//Recipients
$mail->setFrom('anymail@gmail.com', 'My Cool Name');
$mail->AddAddress($email_1, $namatim);     // Add a recipient
$mail->AddAddress($email_2, $namatim);     // Add a recipient
$mail->AddAddress($email_3, $namatim);     // Add a recipient
$mail->addReplyTo('anymail@gmail.com', 'My Cool Name');

//Content
$mail->isHTML(true);                                  //Set email format to HTML
$mail->Subject = 'Verification Success';
$mail->Body    = 'This is a message';

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

从故障排除 page 中,我可以看到这表示为 DNS 问题。我怀疑这是我的 XAMPP 的 DNS 的问题。

我尝试在我的本地机器上执行 dig +short smtp.gmail.com 并且它工作正常但是当我点击 XAMPP 上的打开终端按钮并尝试运行相同的命令时,该命令不起作用。

我想知道需要在我的 php.ini 上更改什么配置,或者其他任何东西(我添加了 extensions=openssl,但仍然出现相同的错误)。

如果有人能解决这个问题并愿意与我分享,我将不胜感激,因为我现在完全无能为力,并且坚持了几个小时。

谢谢。

1 个答案:

答案 0 :(得分:0)

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

您必须将 PHPMailer 类导入全局命名空间, 并且这些必须在脚本的顶部,而不是在函数内。

使用:

$mail = new PHPMailer(true);

您必须在父目录中使用此命令,因为您的 PHPMailer 类将从“供应商”文件夹中执行。

composer dump-autoload

你可以查看我的代码:

https://github.com/mhreza76/php_pondit/blob/main/session_06/public/php-mailer.php