2013-10-30 83 views
0

所以我想用PHPMailer來處理我的網站上的電子郵件表單。郵件發送與PHPMailer不起作用

我在這裏根據我找到的教程寫了代碼。

<?php 
     error_reporting(E_ALL); 
     require_once("class.phpmailer.php"); 
     include("class.smtp.php"); 

     $email = new PHPMailer(); 

     // 
     // Set server details for send 
     // 
     $email->IsSMTP(); 
     $email->Host = "mail.loganyoung.za.net"; 
     $email->Port = 25; 
     $email-SMTPAuth = true; 
     $email->Username = "<my email>"; 
     $email->Password = "<my password>"; 

     // 
     // Send mail from the contact form 
     // 
     $to = "<my email>"; 
     $from = $_POST["from"]; 
     $name = $_POST["name"]; 
     $subject = "From web: ".$_POST["subject"]; 
     $message = $_POST["message"]; 

     $body = "<p>Hi Logan,</p>"; 
     $body .= "<p>You have received a new query on your website.<br />Please see below:</p>"; 
     $body .= "<p>"; 
     $body .= str_replace("\r\n", "<br />", $message); 
     $body .= "</p>"; 

     $email->SetFrom($from, $name); 
     $email->AddReplyTo($from, $name); 
     $email->AddAddress($to, "LoganYoung.za.net"); 
     $email->Subject = $subject; 
     $email->Body = $body; 
     $email->IsHTML = true; 

     session_start(); 
     if(!$email->Send()) {   
       $_SESSION["mailresult"] = "success"; 
       echo "success"; 
     } else { 
       echo "<p>Failed:</p><p>".$email->ErrorInfo."</p>"; 
       $_SESSION["mailresult"] = "failed"; 
       $_SESSION["mailerror"] = $email->ErrorInfo; 
     } 

?> 

我想,可能的原因爲它不發送可能是......

  • 不正確的SMTP信息(可能不發送SMTP認證解決?)
  • 處理程序沒有得到POST數據(發送它的AJAX似乎做工精細,雖然)
  • 一些問題,此代碼,我不能夠確定...

作爲一種消除可能性的方法,任何人都可以在這裏找到代碼的任何錯誤嗎?如果是這樣,有什麼問題,我該如何解決?

在此先感謝!

+0

這個'如果(!$的電子郵件 - >發送()){'也許應該是'如果($的電子郵件 - >發送()) {'沒有'!' - 這就是說'if NOT/NO email ...' - '!'是PHP中的否定符號。 –

+0

斑點。我更新了代碼,但似乎沒有幫助。我在Chrome的開發工具中看到,會話沒有被創建 – Ortund

回答

1
$email-SMTPAuth = true; 

是不是應該是:

$email->SMTPAuth = true;