联系表格不发送电子邮件

时间:2011-04-18 01:41:44

标签: php html contact-form

我正在编写我的第一个PHP联系表单。我整天都在努力。在Google上查找所有内容,我无法得到答案。来自成功页面的联系人仍然没有进入我的收件箱............

PHP:

<?php
  // Define some constants
  define( "RECIPIENT_NAME", "name" );
  define( "RECIPIENT_EMAIL", "name@gmail.com" );
  define( "EMAIL_SUBJECT", "Message" );

  // Read the form values
  $success = false;
  $sender = isset( $_POST['sender'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['sender'] ) : "";
  $email = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
  $message = isset( $_POST['message'] ) ?  preg_replace( "/(From: |To: |BCC: |CC: |Subject: |Content-Type:)/", "", $_POST['message'] ) : "";

  //If all values exist, send the email 
  if ( $sender && $email && $message ) {
    $recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";
    $headers = "From: " . $sender . " <" . $email . ">";
    $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  }

  // Return an appropriate reponse to the browser
  if( isset($_GET["ajax"]) ) {
    echo $success ? "success" : "error";
  } else {
  ?>
  <html>
    <head>
    <title>Thanks!</title>
    </head>
    <body>
      <?php if ( $success ) echo "<h2>Thanks for sending your message! We'll get back to you shortly.</h2>" ?>
      <?php if ( !$success ) echo "<h2> Sorry, There was a problem sending your message. Please try again.</h2>" ?>
      <p> Click your browser's back button to return to the page</p>
    </body>
  </html>
  <?php
  }
  ?>

它有什么问题吗?

继承HTML

<form id="contactForm" action="process.php" method="post"> 
  <label for="sender">Your Name</label> 
  <input type="text" name="sender" id="sender" placeholder="Your Name" required="required" maxlength="40"> 

  <label for="email">Your Email Address</label> 
  <input type="email" name="email" id="email" placeholder="Please type your email address"
  required="required" maxlength="50"> 

  <label for="message">Your Message</label> 
      <textarea name="message" id="message" placeholder="Please type your message" required="required"
       cols="50" rows="5" maxlength="10000"></textarea>     

  <div id="form-buttons"> 
    <input type="submit" id="send-message" name="send-message" value="Send Email" /> 
    <input type="button" id="cancel" name="cancel" value="Cancel" /> 
  </div><!-- #form-buttons --> 

有人可以看一下吗?我确信我做的一切都是对的......

2 个答案:

答案 0 :(得分:0)

至少你的行

$success mail( $recipient, EMAIL_SUBJECT, $message, $headers );

似乎错了,mail之前应该有一个等号。

答案 1 :(得分:0)

更改此行:

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";

为:

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . ">";

可能是问题