我已经编写了一个联系表单,但由于某种原因我实际上并没有收到电子邮件,但是php代码没有错误,表单返回成功消息。我已从此处的代码中删除了我的电子邮件地址。
联系表单
<body>
<div class='formbody'>
<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<form id="contact-form" action="send.php" method="POST">
<p>Hi</p>
<p>My
<label for="your-name">name</label> is
<input type="text" name="your-name" id="your-name" minlength="3" placeholder="(your name here)" required> and</p>
<p>my
<label for="email">email address</label> is
<input type="email" name="your-email" id="email" placeholder="(your email address)" required>
</p>
<p> I have a
<label for="your-message">message</label> for you,</p>
<p>
<textarea name="your-message" id="your-message" placeholder="(your msg here)" class="expanding" required></textarea>
</p>
<p>
<button type="submit">
<svg version="1.1" class="send-icn" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100px" height="36px" viewBox="0 0 100 36" enable-background="new 0 0 100 36" xml:space="preserve">
<path d="M100,0L100,0 M23.8,7.1L100,0L40.9,36l-4.7-7.5L22,34.8l-4-11L0,30.5L16.4,8.7l5.4,15L23,7L23.8,7.1z M16.8,20.4l-1.5-4.3
l-5.1,6.7L16.8,20.4z M34.4,25.4l-8.1-13.1L25,29.6L34.4,25.4z M35.2,13.2l8.1,13.1L70,9.9L35.2,13.2z" />
</svg>
<small>SEND PLEASE</small>
</button>
</p>
</form>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/bs-animation.js"></script>
<script src="assets/js/index.js"></script>
</div>
</body>
</html>
send.php
<?php
$page='contactus';
include('header.php');
include('navbar.php');
?>
<?php
$to = 'removed';
$name = TRIM (stripslashes($_POST['your-name']));
$email = TRIM (stripslashes($_POST['your-email']));
$message = TRIM (stripslashes($_POST['your-message']));
$subject = 'Message';
$name = 'Name: ';
$email= 'From:';
$message= 'Message: ';
if( mail($to, $subject, $name , $email , $message) ){
echo 'success';
}
else{
print('There has been a small error . Sorry!!');
}
?>
答案 0 :(得分:1)
因此,您尚未在此处发送标题或添加标题。
因此,我们不是将表单传递给新文件(send.php),而是将逻辑添加到send.php,并让表单文件在代码顶部需要它,如下所示:
<?php
require 'send.php';
$send = new sendEmail();
?>
<body>
<div class='formbody'>
<?php
if (isset($_POST['sendForm']))
{
$send->sendMail($_POST['your-name'], $_POST['your-email'], $_POST['your-message']);
}
?>
<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<form id="contact-form" action="" method="post">
<p>Hi</p>
<p>My
<label for="your-name">name</label> is
<input type="text" name="your-name" id="your-name" minlength="3" placeholder="(your name here)" required> and</p>
<p>my
<label for="email">email address</label> is
<input type="email" name="your-email" id="email" placeholder="(your email address)" required>
</p>
<p> I have a
<label for="your-message">message</label> for you,</p>
<p>
<textarea name="your-message" id="your-message" placeholder="(your msg here)" class="expanding" required></textarea>
</p>
<p>
<button type="submit" name="sendForm">
<svg version="1.1" class="send-icn" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100px" height="36px" viewBox="0 0 100 36" enable-background="new 0 0 100 36" xml:space="preserve">
<path d="M100,0L100,0 M23.8,7.1L100,0L40.9,36l-4.7-7.5L22,34.8l-4-11L0,30.5L16.4,8.7l5.4,15L23,7L23.8,7.1z M16.8,20.4l-1.5-4.3
l-5.1,6.7L16.8,20.4z M34.4,25.4l-8.1-13.1L25,29.6L34.4,25.4z M35.2,13.2l8.1,13.1L70,9.9L35.2,13.2z" />
</svg>
<small>SEND PLEASE</small>
</button>
</p>
</form>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/bs-animation.js"></script>
<script src="assets/js/index.js"></script>
</div>
</body>
</html>
然后在send.php文件中添加:
<?php
class sendEmail
{
public function sendMail($name, $email, $message)
{
if (!empty($name))
{
if (!empty($email))
{
if (!empty($message))
{
$email_to = 'YOUR EMAIL ADDRESS';
$header = 'From: ' . $name ."<noreply@yourdomain.co.uk/com>". "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, 'Enquiry Received', 'Name: ' . $name . "\r\n\r\n". 'Email: ' .$email. "\r\n\r\n". 'Message: ' .$message."\r\n\r\n", $header);
echo "Thanks for contacting us";
} else {
echo "Please enter a message";
}
} else {
echo "Please provide your email address.";
}
} else {
echo "Please provide your name.";
}
}
}
?>
现在,我添加了条件,如果字段为空,它将返回错误,这完全是因为可以绕过添加到输入字段中的required
,因此它始终是好的也有条件。
注意:如果您希望将表单查询等存储到您的数据库中,例如从任何IP地址发送,发送时间,来自谁,什么,您还可以在此处添加逻辑消息说。
这只是一个基本的例子,但是你会看到一旦读完代码就完成了。