【问题标题】:HTML form isn't emailingHTML 表单没有通过电子邮件发送
【发布时间】:2013-06-28 16:01:30
【问题描述】:

我有这个作为我的表单

        <div class="contactInputs">
                <p>Send us a message</p>
                <form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
                  <input type="text" name="name" placeholder="Name*" required>
                  <input type="text" name="companyname" placeholder="Company Name">
                  <input type="email" name="email" placeholder="Email*" required>
                    <input type="tel" name="phone" placeholder="Phone">
                    <input class="contact-submit" type="submit">
                </form>
                <textarea type="textarea" name="message" placeholder="Your Messages*" required></textarea>
                </div>

这是用于发送电子邮件的 PHP。

<?php
if(isset($_POST['email'])) {

$email_to = "email@gmail.com";
$email_subject = "Your email subject line";


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['companyname']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$name = $_POST['name'];
$companyname = $_POST['companyname'];
$email_from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['comments'];

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$companyname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($message) < 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 .= "First Name: ".clean_string($name)."\n";
$email_message .= "Last Name: ".clean_string($companyname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "phone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($message)."\n";


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


Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

但是每当我尝试提交它时,我都会收到错误

非常抱歉,您提交的表单存在错误。这些错误出现在下方。

很抱歉,您提交的表单似乎有问题。

请返回并修复这些错误。

有没有人看错了

【问题讨论】:

    标签: php html forms email


    【解决方案1】:

    您的表单中没有“cmets”输入。

    【讨论】:

      【解决方案2】:

      php if 语句正在寻找您没有的 cmets 字段。这使您的 if 语句为真,并杀死 php 程序。

      if(!isset($_POST['name']) ||
        !isset($_POST['companyname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone']) ||
      //It's looking for comments 
        !isset($_POST['comments'])) {
          //Your error message
          died('We are sorry, but there appears to be a problem with the form you submitted.');       
      }
      

      您可以通过在表单中​​包含“cmets”部分来解决此问题,或者将其完全从 php 中删除,这样它就不会出现这种情况。

      您似乎正在尝试使用名为“message”的元素来完成类似于“cmets”表单元素的操作,但是,该元素位于正在提交的表单之外。如果您想让“消息”成为表单的一部分,请确保它位于 &lt;form&gt; 标记内。

      <form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
          <input type="text" name="name" placeholder="Name*" required>
          <input type="text" name="companyname" placeholder="Company Name">
          <input type="email" name="email" placeholder="Email*" required>
          <input type="tel" name="phone" placeholder="Phone">
          <!--New placement of message element-->
          <textarea type="textarea" name="message" placeholder="Your Messages*" required></textarea>
          <input class="contact-submit" type="submit">
      </form>
      

      此外,如果您保留“消息”元素的名称而不是将其更改为注释,请记住修复 php 代码以反映该更改。代码中有两个地方当前引用了“comment”元素:

      上面显示的那个,以及当你定义你的变量时。

      $name = $_POST['name'];
      $companyname = $_POST['companyname'];
      $email_from = $_POST['email'];
      $phone = $_POST['phone'];
      //refers to comments
      $message = $_POST['comments'];
      

      我希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        您的表单中似乎没有名为comments 的输入字段。此外,您的 textarea 不在您的表格中,我想您的意思是命名为 comments。将您的 HTML 更改为:

        <form class="messageForm" autocomplete="on" name="contactform" method="post" action="/freequote.php">
            <input type="text" name="name" placeholder="Name*" required>
            <input type="text" name="companyname" placeholder="Company Name">
            <input type="email" name="email" placeholder="Email*" required>
            <input type="tel" name="phone" placeholder="Phone">
            <textarea type="textarea" name="comments" placeholder="Your Messages*" required></textarea>
            <input class="contact-submit" type="submit">
        </form>
        

        【讨论】:

          【解决方案4】:
           !isset($_POST['comments']))
          

          此字段在您的 HTML 中不存在。你有一个“消息”字段,所以也许这应该是:

           !isset($_POST['message']))
          

          【讨论】:

            猜你喜欢
            • 2016-03-10
            • 1970-01-01
            • 1970-01-01
            • 2013-01-20
            • 2022-10-23
            • 1970-01-01
            • 1970-01-01
            • 2014-02-12
            • 1970-01-01
            相关资源
            最近更新 更多