php - PHPMailer 错误:php_network_getaddresses:getaddrinfo 失败:LOCALHOST(XAMPP)中的名称解析暂时失败
问题描述
首先,我在 Mac OS 上使用 XAMPP 和最新的 PHP 邮件程序,我知道这可能与 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}";
}
从故障排除页面中,我可以看到这被指示为 DNS 问题。我怀疑这是我的 XAMPP 的 DNS 的问题。
我试过dig +short smtp.gmail.com
在我的本地机器上做,它工作正常,但是当我点击 XAMPP 上的打开终端按钮并尝试运行相同的命令时,该命令不起作用。
我想知道在我的 php.ini 上需要更改哪些配置,或者其他任何东西(我添加了 extensions=openssl,但仍然出现相同的错误)。
如果有人有办法解决这个问题并愿意与我分享,我将非常感激,因为我现在完全一无所知并且被困了几个小时。
谢谢。
解决方案
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
推荐阅读
- algorithm - 递归关系的时间复杂度:T(n) = nT(n^1/2)+ O(1)
- scala - 从 Spark 中的 Driver 访问任务进度
- ios - 如何修复 iOS 崩溃“com.apple.main-thread SIGABRT ABORT”
- mongodb - 在 Express 中哪里可以找到 Mongo Collection?
- perl - Perl 试图理解正则表达式调试输出
- android - 我可以安全地从我的 google-services.json 文件中删除除当前应用程序数据之外的所有应用程序数据吗?
- angular - Ngx 芯片填充
不发布 HTML 对象 onFocus of input - swift - MVVM 中的 Api 到业务模型的转换
- java - 尝试检索 json 节点时出现 ClassCastException
- javascript - 如何将 JS 链接到 SAP API 开发工作