我的联系表格不愿意发送电子邮件。没有给出错误,但也没有收到邮件(我不在本地服务器上工作)。
我使用面向对象的方式,而不是最简单的方式,仍然可以工作..我的猜测是mail()函数根本没有被触发,但我不知道理解为什么。
非常感谢所有帮助!
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 :(得分:1)
您的问题不在PHP类中。它不会抛出错误,因为它没有收到任何$_POST
数据。您需要从enctype="text/plan"
中移除<form>
。
完成后应该是这样的:
<form name="contactform" action="" method="post">
答案 1 :(得分:0)
在测试表单之前,您是否已经设置了smtp / sendmail?
您可以使用Library 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();