谷歌phpmailer错误SMTP错误:无法连接到SMTP主机


google phpmailer error SMTP Error: Could not connect to SMTP host

我正在尝试使用googlephp mailer,但我收到了很多错误消息。我已经上传了一个新的副本来再次测试,我得到了这个错误:

SMTP Error: Could not connect to SMTP host

调试后,我得到了这个消息

SMTP -> ERROR: Failed to connect to server: Network is unreachable (101) 
SMTP Error: Could not connect to SMTP host

我已从gmail启用IMAP和POP转发并打开此页面https://accounts.google.com/DisplayUnlockCaptcha单击"继续"并刷新了要发送的页面,但它不起作用。我收到邮件错误:SMTP Connect()失败。

这是代码:

<?php
require_once('class.phpmailer.php');
define('GUSER', 'username@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) { 
global $error;
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; 
$mail->Username = GUSER;  
$mail->Password = GPWD;           
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo; 
    return false;
} else {
    $error = 'Message sent!';
    return true;
}
}
$msg = 'Hello World';
$subj = 'test mail message';
$to = 'username@yahoo.com';
$from = 'username@gmail.com';
$name = 'any name';
if (smtpmailer($to, $from, $name, $subj, $msg)) {
echo 'Yippie, message send via Gmail';
} else {
if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
    if (!empty($error)) echo $error;
} else {
    echo 'Yep, the message is send (after doing some hard work)';
}
}
?>

如何解决这个问题?提前感谢

检查服务器上的php.ini文件。验证openssl.dll行是否已取消注释