php表单不发送联系电子邮件

Posted

技术标签:

【中文标题】php表单不发送联系电子邮件【英文标题】:php form not sending contact email 【发布时间】:2019-09-04 12:16:30 【问题描述】:

我正在开发新的网络并尝试创建联系表单。

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


?>

表格:

    <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 going wong”消息。 但是当我配置并尝试使用普通的 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 >
<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>

我尝试了许多在线解决方案,但无法修复它。 提前感谢任何解决方案。

【问题讨论】:

您在发送电子邮件之前检查$_POST['email'],但第一个表单没有任何名为email 的输入/按钮,而第二个表单有。 type="email_fld"..... 解决这个问题 【参考方案1】:

试试这个代码(保存为单个文件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 >
    <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 没有将我的联系表单输入发送到我的电子邮件?

使用 PHP 和 AJAX 发送电子邮件表单

使用PHP和特殊字符从联系表单发送邮件[重复]

没有 PHP 的电子邮件联系表

如何使用本地 xampp 和 php 联系人表单从我的 mac 发送邮件

多上传附件联系表单