簡體   English   中英

PHP的形式不發送聯系人電子郵件

[英]php form not sending contact email

我是新開發的網站,正在嘗試創建聯系表。

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>

每當我按下“提交”按鈕時,它就會顯示默認的“消息已消失”。 但是,當我配置並嘗試使用普通的HTML Form時,代碼工作正常。

普通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.\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模式屬性 在這種情況下,您不會用服務器端字段檢查來煩擾您的沖浪者。 +如果您不添加人工檢查驗證碼,此表格對於垃圾郵件發送者是一個很好的解決方案。

暫無
暫無

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

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