2015-10-06 46 views
0

我的聯繫表不願發送電子郵件。沒有給出錯誤,但沒有收到郵件(我沒有在本地服務器上工作)。我使用的是面向對象的方式,而不是最簡單的方法,它仍然應該工作..我的猜測是,郵件()函數根本沒有觸發,但我不明白爲什麼。PHP面向對象的聯繫表

所有幫助深表感謝!

contact.php

<?php 

include_once('classes/sendmail.php'); 

if(isset($_POST['sendmessage'])){ 
    try { 
     $e = new Email(); 
     $e->Name = $_POST['name']; 
     $e->Email = $_POST['email']; 
     $e->Message = $_POST['message']; 
     $e->Send(); 
    } 

    catch(Exception $e){ 
     $error = $e->getMessage(); 
    } 
} 

if(isset($error)){ 
    echo $error; 
} 
?> 


<!doctype html> 
<body> 


      <form name="contactform" action="" method="post" enctype="text/plain"> 
        <label for="name">Name</label> 
        <input type="text" name="name" id="name" class="name" /> 

        <label for="email">E-mail</label> 
        <input type="text" name="email" id="email" class="email" /> 

        <label for="message">Message</label> 
        <textarea id="message" name="message" ></textarea> 

       <input type="submit" class="send" value="Verzend" name="sendmessage" /> 
      </form> 

類/ sendmail.php

<?php 

class Email{ 

    private $m_sName; 
    private $m_sEmail; 
    private $m_sMessage; 

    public function __set($p_sProperty, $p_vValue){ 
     switch($p_sProperty){ 

      case 'Name': 
       if($p_vValue != '') { 
        $this->m_sName = $p_vValue; 
       } 
       else{ 
        throw new Exception("No name found"); 
       } 
       break; 

      case 'Email': 
      if($p_vValue != '') { 
        $this->m_sEmail = $p_vValue; 
       } 
       else{ 
        throw new Exception("No email adress found"); 
       } 
       break; 

      case 'Message': 
       if($p_vValue != '') { 
        $this->m_sMessage = $p_vValue; 
       } 
       else{ 
        throw new Exception("No message found"); 
       } 
       break; 
     } 
    } 

    public function __get($p_sProperty){ 
     switch($p_sProperty){ 
      case 'Name': 
       return $this->m_sName; 
       break; 

      case 'Email': 
       return $this->m_sEmail; 
       break; 

      case 'Message': 
       return $this->m_sMessage; 
       break; 
     } 
    } 

    public function Send(){ 

     $email_to = "<my Email>"; 
     $email_subject = "<my subject>"; 

     $email_message = "Name: ".$this->m_sName."\n"; 
     $email_message .= "Email: ".$this->m_sEmail."\n"; 
     $email_message .= "Message: ".$this->m_sMessage."\n"; 

     $headers = 'From: '.$this->m_sEmail."\r\n". 
     'Reply-To: '.$this->m_sEmail."\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 

     mail($email_to, $email_subject, $email_message, $headers); 

    } 

} 

?> 

編輯:有很多問題已經被問及接觸的形式,但沒有一個是使用OOP似乎像我一樣有同樣的問題。

+0

當你在send()函數內部回顯時會發生什麼? – Exception

+0

回聲究竟是什麼?在頁面頂部彈出任何內容。 – lvRoy

回答

1

你的問題不在PHP類中。它不會拋出錯誤,因爲它沒有收到任何$_POST數據。您需要從<form>中刪除enctype="text/plan"

應該是這樣做的時候:

<form name="contactform" action="" method="post"> 
+0

這樣做,非常感謝! – lvRoy

+0

你打賭 - 高興地幫助 – visevo

0

你已經測試你的表格前,設置SMTP/sendmail的?

您可以使用圖書館的PHPMailer

例子:

function send_email($subject, $from_mail, $message) { 

      $mail = new PHPMailer(); 
      $mail->IsSMTP(); // send via SMTP 
      $mail->Host  = setting_db('SMTPHOST'); // SMTP servers 
      $mail->SMTPAuth = true;     // turn on 
      $mail->Username = setting_db('SMTPUSER'); // SMTP username 
      $mail->Password = setting_db('SMTPPASSWORD'); // SMTP password 
      $mail->From  = setting_db('SMTPUSER'); // // SMTP username from 
      $mail->FromName = "Your Name"; 
      $mail->AddAddress($from_mail); // 
      $mail->Subject = $subject; 
      $mail->Body  = $message; 
      if ($mail->send()) { 
       return "OK!"; 
      } else { 
       return "No!". $mail->ErrorInfo; 
      } 

}

和更新文件contact.php

send_email($_POST["subject"], $_POST["email"], $_POST["message"]); 

更換

$e = new Email(); 
$e->Name = $_POST['name']; 
$e->Email = $_POST['email']; 
$e->Message = $_POST['message']; 
$e->Send(); 
+0

這並不完全回答他的問題。 – visevo