簡體   English   中英

HTML表單不通過電子郵件發送

[英]HTML form isn't emailing

我有這個作為我的表格

        <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 if語句正在查找您沒有的注釋字段。 這使你的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.');       
}

你可以通過在表單中​​包含一個“注釋”部分,或者從php中完全刪除它來解決這個問題,這樣它就不會期望它。

您似乎嘗試使用名為“message”的元素來完成類似於“comments”表單元素的操作,但是,此元素位於正在提交的表單之外。 如果您希望將“message”作為表單的一部分,請確保它位於<form>標記內。

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

此外,如果您保留“message”元素的名稱而不是將其更改為注釋,請記住修復php代碼以反映該更改。 代碼中有兩個地方當前引用了“comment”元素:

如上所示,以及定義變量時。

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

我希望這有幫助!

您的表單中似乎沒有名為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>
 !isset($_POST['comments']))

您的HTML中不存在此字段。 你有一個“消息”字段,所以這應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM