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

时间:2013-08-25 18:24:29

标签: php smtp phpmailer

我正在尝试使用google php邮件程序,但我收到了很多错误消息。我上传了一个新的副本再次测试,我收到了这个错误:

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)';
}
}
?>

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

1 个答案:

答案 0 :(得分:1)

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