PHP用于电子邮件联系表单

时间:2012-03-22 19:37:10

标签: php html forms email contact

我的PHP代码有问题,可以通过联系表单发送电子邮件。我没有得到任何错误,并且在彻底研究之后可以看到代码没有问题,有人可以帮助我吗?

<?php

// Define some constants
define( "RECIPIENT_NAME", "Nuim Buzz" );
define( "RECIPIENT_EMAIL", "nuimbuzz@gmail.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

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

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


?>
<html>
  <head>
    <title>Thanks!</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  <p>Click your browser's Back button to return to the page.</p>
  </body>
</html>

HTML代码:                        

  <h2>Send us an email...</h2>

  <ul>

    <li>
      <label for='senderName'>Your Name</label>
      <input type='text' name='senderName' id='senderName' placeholder='Please type your name...' required='required' maxlength='40' />
</li>

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

<li>
  <label for='message' style='padding-top: .5em;'>Your Message</label>
  <textarea name='senderMessage' id='message' placeholder='Please type your message...' required='required' cols='80' rows='10' maxlength='10000'></textarea>
</li>

  </ul>

  <div id='formButtons'>
    <input type='submit' id='sendMessage' name='sendMessage' value='Send Email' />

  </div>

</form>

3 个答案:

答案 0 :(得分:2)

看来你的问题是服务器设置。你是当地人吗?您是否在共享主机方案中?如果后者可能存在域名问题,请联系您的托管解决方案以确保您已将其设置正确。另请检查服务器错误日志。

答案 1 :(得分:0)

我的第一个猜测是你在本地运行,在这种情况下你需要设置一个本地邮件服务器。

此外,if ( $senderName && $senderEmail && $senderMessage )如果其中任何变量(例如$senderMessage)为"0",则会失败。

答案 2 :(得分:0)

我找不到<form>。可能会引起问题。