2017-06-28 988 views
-1

我想在我的項目中使用phpmailer,但Gmail的SMTP設置似乎並不正確。我提到了前面提到的幾個問題,但沒有一個解決方案似乎適用於我。PHPMailer-郵件程序錯誤:SMTP連接()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting?

<?php 
    require 'PHPMailer-master/PHPMailerAutoload.php'; 
    $mail = new PHPMailer; 
    $mail->IsSMTP();         
    $mail->Host = 'smtp.gmail.com';     
    $mail->Port = 587;       
    $mail->SMTPSecure = 'tls'; 
    $mail->SMTPAuth = true;      
    $mail->Username = ''; //gmail address     
    $mail->Password = ''; // gmail password 
    $mail->From = '[email protected]'; 
    $mail->FromName = 'John Doe'; 
    $mail->AddAddress('[email protected]', 'Nick'); // Add a recipient 
    $mail->IsHTML(true);        // Set email format to HTML 
    $mail->Subject = 'subject'; 
    $mail->Body = 'message body'; 
    $mail->AltBody = 'This is a plain-text message body'; 
    $mail->addAttachment('images/apache_pb.png'); 
    if (!$mail->send()) { 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
     echo "Message sent!"; 
    } 
?> 
+0

什麼是錯誤? – Webinion

+0

這個託管在godaddy上? – developerCK

+0

PHPMailer-郵件程序錯誤:SMTP連接()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting –

回答

0

您必須安裝php_openssl.dll,如果您使用wampserver,這很容易,搜索和應用PHP的擴展。

在這個例子中更改此:

//Set the hostname of the mail server 
    $mail->Host = 'smtp.gmail.com'; 

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl 
    $mail->Port = 465; 

    //Set the encryption system to use - ssl (deprecated) or tls 
    $mail->SMTPSecure = 'ssl'; 

,然後從Gmail recived電子郵件談論啓用該選項在這裏不太安全訪問應用程序https://www.google.com/settings/security/lesssecureapps

+0

非常感謝。有效。但是,這將在像Godaddy或亞馬遜這樣的現場服務器上工作嗎? –

+0

是的,可能會起作用。 –

+0

Ookey ..謝謝:) –

0

使用

$mail->Host = 'mail.servce.com'; 
+1

你能詳細說明你的答案嗎?發表一段沒有任何文字的代碼往往不是很有幫助。 –

相關問題