我一直在尝试使用phpmailer类测试从php发送电子邮件。我的配置是这样的:
require '../php/library/class.phpmailer.php';
require '../php/library/class.smtp.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "anactual@email.com";
$mail->Password = "thepassword";
$mail->setFrom('existing@gmail.com', 'Real Name');
$mail->addAddress( $validEmail );
$mail->Subject = 'Confirmation Code';
$mail->msgHTML('Follow this link: <a href="http://test.com/confirm?code=' . $newAccount->confirmationCode . '">http://test.com/confirm?code=' . $newAccount->confirmationCode . '</a>');
$mail->AltBody = 'This is a plain-text message body with this link: http://test.com/confirm?code=' . $newAccount->confirmationCode;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
然而,事情似乎不起作用,我只是复制粘贴gmail示例中的代码。我在万维网上搜索了错误输出:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
但仍然无法找出错误。有什么帮助吗?
更新我已在gmail中停用了两步验证
答案 0 :(得分:0)
尝试使用phpmailer提供的gmail示例代码,而不是您使用过的旧代码。 https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
然后查看此处的问题排查指南https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting