2013-03-30 78 views
0

我試圖通過PHPMailer的從我的Gmail發送郵件,但我得到了以下錯誤:PHPMailer的 - 無法連接到SMTP

SMTP Error: Could not connect to SMTP host.

我下載的PHPMailer類從:https://github.com/Synchro/PHPMailer

我試了超過三十個小時,所以我試過了端口25,465,587的大部分組合。我甚至嘗試過從我的SMTP用戶名中刪除@ gmail.com這樣的奇怪東西,所以請幫助我。

<?php 
/** 
* Simple example script using PHPMailer with exceptions enabled 
* @package phpmailer 
* @version $Id$ 
*/ 

require '../class.phpmailer.php'; 

try { 
    $mail = new PHPMailer(true); //New instance, with exceptions enabled 

    $body    = file_get_contents('contents.html'); 
    $body    = preg_replace('/\\\\/','', $body); //Strip backslashes 

    $mail->IsSMTP();       // tell the class to use SMTP 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = 'ssl'; 
    $mail->Port  = 465;     // set the SMTP server port 
    $mail->Host  = "smtp.gmail.com"; // SMTP server 
    $mail->Username = "[email protected]";  // SMTP server username 
    $mail->Password = "mypass";   // SMTP server password 

// $mail->IsSendmail(); // tell the class to use Sendmail 

    $mail->AddReplyTo("[email protected]in.com","First Last"); 

    $mail->From  = "[email protected]"; 
    $mail->FromName = "First Last"; 

    $to = "[email protected]"; 

    $mail->AddAddress($to); 

    $mail->Subject = "First PHPMailer Message"; 

    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
    $mail->WordWrap = 80; // set word wrap 

    $mail->MsgHTML($body); 

    $mail->IsHTML(true); // send as HTML 

    $mail->Send(); 
    echo 'Message has been sent.'; 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); 
} 
?> 
+0

查看此文章:https://stackoverflow.com/questions/37079061/phpmailer-saying-could-not-connect-to-smtp-host –

回答

0

我試圖通過PHP的郵件從我的網頁電子郵件和我同樣的問題..然後我上傳我的代碼在服務器上,然後它的工作。我認爲PHPMailer的從Web服務器的工作原理,它不會工作從本地主機的機器...嘗試將其上傳於網絡服務器的代碼,然後嘗試...如果你的代碼被上傳的服務器上,然後使用這個....

<?php 

     require_once "../class.phpmailer.php"; 

     $from = "<from.gmail.com>"; 
     $to = "<to.gmail.com>"; 
     $subject = "Hi!"; 
     $body = "Hi,hows goin??"; 

     $host = "ssl://smtp.gmail.com"; 
     $port = "465"; 
     $username = "[email protected]"; //<> give errors 
     $password = "password"; 

     $headers = array ('From' => $from, 
      'To' => $to, 
      'Subject' => $subject); 
     $smtp = Mail::factory('smtp', 
      array ('host' => $host, 
      'port' => $port, 
      'auth' => true, 
      'username' => $username, 
      'password' => $password)); 

     $mail = $smtp->send($to, $headers, $body); 

     if (PEAR::isError($mail)) { 
      echo("<p>" . $mail->getMessage() . "</p>"); 
     } else { 
      echo("<p>Message successfully sent!</p>"); 
     } 

    ?> 
+0

該代碼上傳到one.com託管的域名。 – user1459650

+0

我收到: 致命錯誤:Class'Mail'未找到/customers/1/6/f/contactfact.com/httpd.www/PHPMailer-master/test/testemail.php 18行 – user1459650

+0

我可以得到您需要的文件? – user1459650

0

的錯誤,我想你正在是你要求錯誤的類,你的要求應該加載 require_once '../PHPMailerAutoload.php'; PHPMailer類依靠o其他子類可以處理髮送郵件的其他方面。你需要的類更多的是一個包裝類,這應該修復許多錯誤,或者至少給你更好的錯誤信息。

相關問題