2017-07-19 123 views
1

我剛剛開始使用PHP進行編碼。我試圖發送郵件,但它會引發錯誤郵件錯誤:SMTP連接()失敗。本地主機上的SMTP連接()失敗錯誤

下面是我使用的代碼:

date_default_timezone_set('Etc/UTC'); 

    require 'PHPMailerAutoload.php'; 

    $mail = new PHPMailer; 

    $mail->isSMTP(); 
    $mail->Host = gethostbyname('ssl://smtp.gmail.com'); 

    $mail->SMTPAuth = true; 
    $mail->AuthType = 'LOGIN'; 
    $mail->Username = '[email protected]'; 
    $mail->Password = '********'; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Port = 587; 

    $mail->setFrom('[email protected]','Abc') ; 
    $mail->addReplyTo('[email protected]'); 
    $mail->addAddress("[email protected]",'No REPLY'); 
    $mail->isHTML(true); 

    $mail->Body = "12345"; 
    $mail->Subject = "<b>OTP for password reset<b>"; 
    $mail->Body = "Hi! Your OTP is : "; 

    if(!$mail->Send()) { 
     echo 'Message was not sent.'; 
     echo "Mailer Error: " . $mail->ErrorInfo; 
     } 
    else { 
     echo 'Message has been sent.'; 
    } 

任何幫助,將不勝感激。

+0

這是哪個版本的php? – abeyaz

+0

檢查此:[與PHPMailer處理錯誤](https://stackoverflow.com/questions/2386544/error-handling-with-phpmailer) –

+0

PHP版本是5.6.25 –

回答

0

變化從

$mail->Host = gethostbyname('ssl://smtp.gmail.com'); 
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587; 

$mail->Host = "smtp.gmail.com"; 
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465; 

併爲您open_ssl啓用

打開php.ini並搜索這條線 ;extension=php_openssl.dll和之前刪除分號,保存該文件並重新啓動您的網絡服務器。

+0

C:\ wamp64 \ www \ cpms \ class.phpmailer.php PHP.INI中的open_ssl已啓用。 我以前使用的代碼,但仍然有相同的錯誤。 $ mail-> Host =「ssl://smtp.gmail.com」; –

+0

哪個錯誤得到? –

+0

SMTP連接()失敗 –

相關問題