0
我想使用谷歌PHP郵件,但我收到了很多錯誤消息。我已經上傳了一個新的副本再次測試和我得到這個錯誤:谷歌phpmailer錯誤SMTP錯誤:無法連接到SMTP主機
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
我已經啓用IMAP和POP轉發來自Gmail並打開這個頁 https://accounts.google.com/DisplayUnlockCaptcha 點擊繼續並刷新頁面發送和它不工作我得到郵件錯誤:SMTP連接()失敗。
下面的代碼:
<?php
require_once('class.phpmailer.php');
define('GUSER', '[email protected]'); // 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 = '[email protected]';
$from = '[email protected]';
$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)';
}
}
?>
如何解決這個問題呢?在此先感謝
您是否嘗試過端口25? –
嘗試$ mail-> Host ='ssl://smtp.gmail.com'; –
將端口更改爲25無法正常工作,並且ssl://smtp.gmail.com也無法正常工作 –