php表单不发送联系电子邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php表单不发送联系电子邮件相关的知识,希望对你有一定的参考价值。
我是新的网络开发者,并试图创建一个联系表单。
php代码:
<?php
if(isset($_POST['email'])) {
$email_to = "myemail@gmail.com";
$email_subject = "Working Fine!";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email_fld']) ||
!isset($_POST['telephone']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email = $_POST['email_fld'];
$telephone = $_POST['telephone'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_fld)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$subject)) {
$error_message .= 'The subject you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($msg) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.
";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."
";
$email_message .= "Email: ".clean_string($email_fld)."
";
$email_message .= "Telephone: ".clean_string($telephone)."
";
$email_message .= "Subject: ".clean_string($subject)."
";
$email_message .= "Message: ".clean_string($msg)."
";
$headers = 'From: '.$email_fld."
".
'Reply-To: '.$email_fld."
" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message, $headers);
?>
<?php
}
?>
表格:
<div class="contact-primary">
<h3 class="h6">Send Us A Message</h3>
<form name="contactForm" id="contactForm" method="post" action="cont.php" novalidate="novalidate">
<fieldset>
<div class="form-field">
<input name="name" type="text" id="name" placeholder="Your Name" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="email_fld" type="email_fld" id="email_fld" placeholder="Your Email" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="telephone" type="text" id="telephone" placeholder="Telephone Number" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="subject" type="text" id="subject" placeholder="Subject" value="" class="full-width">
</div>
<div class="form-field">
<textarea name="msg" id="msg" placeholder="Your Message" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button id="submit" type="submit" name="submit" class="full-width btn--primary">SEND</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
<div class="message-warning">
Something went wrong. Please try again.
</div>
<div class="message-success">
Your message was sent, thank you!<br>
</div>
</div>
每当我按下提交按钮时它显示默认的“Something goes wong message”。但是当我使用普通的html表单进行配置和尝试时,代码运行正常。
普通HTML表单:
<div class="container-fluid contact-form"><!-- contact form -->
<center style="font-size:50px; font-weight:bold;">MESSAGE US</center>
<form name="contactform" method="post" action="cont.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>
我尝试了很多在线解决方案,但无法修复它。在此先感谢任何解决方案。
答案
试试这段代码(将其保存为单个文件cont.php
):
<?php
if(isset($_POST['email'])) {
$email_to = "myemail@gmail.com";
$email_subject = "Working Fine!";
if(!isset($_POST['name']) ||
!isset($_POST['email_fld']) ||
!isset($_POST['telephone']) ||
!isset($_POST['subject']) ||
!isset($_POST['comments'])) {
died('We are sorry, but all fields is necessary.');
}
$name = $_POST['name'];
$email = $_POST['email_fld'];
$telephone = $_POST['telephone'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_fld)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$subject)) {
$error_message .= 'The subject you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($msg) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.
";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."
";
$email_message .= "Email: ".clean_string($email_fld)."
";
$email_message .= "Telephone: ".clean_string($telephone)."
";
$email_message .= "Subject: ".clean_string($subject)."
";
$email_message .= "Message: ".clean_string($msg)."
";
$headers = 'From: '.$email_fld."
".
'Reply-To: '.$email_fld."
" .
'X-Mailer: PHP/' . phpversion();
if (mail($email_to, $subject, $email_message, $headers)) {
echo '<div class="message-success">Your message was sent, thank you!</div>';
} else {
echo '<div class="message-warning">Something went wrong. Please try again.</div>';
}
exit;
}
?>
<div class="container-fluid contact-form"><!-- contact form -->
<center style="font-size:50px; font-weight:bold;">MESSAGE US</center>
<form name="contactform" method="post" action="cont.php">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</div>
此代码只是基于您的代码的示例。我没有为die()
消息插入格式。我建议你在客户端插入字段检查html表单:HTML pattern Attribute。在这种情况下,您不会通过服务器端字段检查来骚扰您的冲浪者。 +如果你不添加人工检查CAPTCHA,这个表格是垃圾邮件发送者的好方法。
以上是关于php表单不发送联系电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 php 没有将我的联系表单输入发送到我的电子邮件?