我的聯繫表不願發送電子郵件。沒有給出錯誤,但沒有收到郵件(我沒有在本地服務器上工作)。我使用的是面向對象的方式,而不是最簡單的方法,它仍然應該工作..我的猜測是,郵件()函數根本沒有觸發,但我不明白爲什麼。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似乎像我一樣有同樣的問題。
當你在send()函數內部回顯時會發生什麼? – Exception
回聲究竟是什麼?在頁面頂部彈出任何內容。 – lvRoy