PHP的形式不发送联系人电子邮件

时间:2019-04-14 08:51:38

标签: javascript php html css

我是新开发的网站,正在尝试创建联系表。

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.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_fld)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($msg)."\n";

$headers = 'From: '.$email_fld."\r\n".
'Reply-To: '.$email_fld."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message, $headers);
?>

<?php

}
?>

FORM:

    <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>

每当我按下“提交”按钮时,它就会显示默认的“消息已消失”。 但是当我配置并尝试使用普通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>

我尝试了许多在线解决方案,但无法解决。 预先感谢您提供任何解决方案。

1 个答案:

答案 0 :(得分:0)

尝试以下代码(将其保存为单个文件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.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_fld)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Message: ".clean_string($msg)."\n";

    $headers = 'From: '.$email_fld."\r\n".
    'Reply-To: '.$email_fld."\r\n" .
    '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。在这种情况下,您不会用服务器端字段检查来烦扰您的冲浪者。 +如果您不添加人工检查验证码,此表格对于垃圾邮件发送者来说是一个很好的解决方案。