【发布时间】:2021-08-23 17:21:30
【问题描述】:
首先,我在 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}";
}
从故障排除page 中,我可以看到这是一个 DNS 问题。我怀疑这是我的 XAMPP 的 DNS 的问题。
我尝试在本地计算机上执行dig +short smtp.gmail.com,但当我单击 XAMPP 上的“打开终端”按钮并尝试运行相同的命令时,它工作正常,但该命令不起作用。
我想知道我的 php.ini 上需要更改哪些配置,或者其他什么(我添加了 extensions=openssl,但仍然遇到同样的错误)。
如果有人有办法解决此问题并愿意与我分享,我将不胜感激,因为我现在完全一无所知并且被困了几个小时。
谢谢。
【问题讨论】:
-
是的,您遇到了 DNS 问题。 PHPMailer 不是尝试修复或诊断它的正确工具。在 XAMPP 中进行挖掘,PHPMailer 将能够工作。我将从 XAMPP 文档开始。
标签: php email xampp smtp phpmailer