php不使用电子邮件表单

时间:2014-03-10 15:30:14

标签: php

我的电子邮件表单似乎不想工作它只是不断回来的PHP表单而不是它应该的消息 这是表单的html部分

 <div class="row">
            <div class="col-sm-12"> 
                <p>The band would love to hear form you!</p>
            </div>  
        </div>
        <div class="row moveMe3">
            <div class="col-sm-12">
                <div id="contact-form"> 
                    <form id="contact" method="post" action="process.php">
                        <fieldset>  
                            <label for="name">Full Name</label>
                            <input type="text" id="name" name="name" placeholder="Full Name" title="Enter your name" class="required">
                            <label for="email">E-mail</label>
                            <input type="email" id="email" name="email" placeholder="yourname@domain.com" title="Enter your e-mail address" class="required email">
                            <label for="message">Question/Comment</label>
                            <textarea name="message" id="message"></textarea>
                            <input type="submit" name="submit" class="btn btn-success btnMove" id="submit" value="Send Message" />
                        </fieldset>
                    </form>
                </div>
                </div><!-- /end #contact-form -->

            <div id="response"></div>
        </div>
        <div class="contactus" id="moveMe2">
            <img src = "images/envalope.jpg" alt="Contact" class="img-responsive" />
        </div>

这是第一个配合它的PHP

<?php
// Get Data 
$name = check_input($_POST['name'], "Please Enter Your Name!<a href='javascript:history.back(1);'>Back</a>");
$email = check_input($_POST['email'], "Please Enter Your Email Address!<a href='javascript:history.back(1);'>Back</a>");
$message = check_input($_POST['message'], "Please Enter Your Message!<a href='javascript:history.back(1);'>Back</a>");


//Email Validation

    $email = htmlspecialchars($_POST['email']);
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
    {
      die("We're Sorry, but something went wrong please chenck it over and try again.");
    }

  //Function Check to Eliminate Unwanted Characters, Strips Quotes, and Adds HTMLSpecialCharacters

    function check_input($data, $problem='')
    {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      if ($problem && strlen($data) == 0)
      {
         die($problem);
      }
      return $data;
    }

  //Spammer Check

    $email = urldecode($email);
    if (eregi("\r",$email) || eregi("\n",$email))
    {
      die("Spammer Detected");
    }

  //Sends Email and Forwards to Thank You Page

    mail( "preshesgirl@yahoo.com", $subject, $message, "From: $name <$email>" );
    header( "Location: thankyou.php" );

?>

这是与之相关的第二个PHP

<?php
include('header.php');
?>
<?php
include('nav.php');
?>
        <div class="row">
            <div class="col-sm-12">
                <div id="response"></div> 
        <div class="contactus" id="moveMe2">
          <h1>We will get back to you soon!</h1>
             <img src="images/thanks.jpg" title="Thank you" alt="Thank you" class="img-responsive">

            </div>
        </div>
    </div>
<?php
include('footer.php');
?>  

2 个答案:

答案 0 :(得分:0)

我使用php邮件功能处理一些大型应用程序。以下是2分:

1)总是尝试使用像swiftmailer或phpmailer这样的库。它们具有终极功能,易于调试。我知道你的学校申请可能很小,不需要图书馆。所以我把这个判断留给你了。

2)mail()返回布尔值。所以如果你使用类似的东西:

try{
    @ mail(......);
} catch (Exception $e){} 

这不会帮助您捕获错误。它只会说服务器是否接受处理您的请求。它可能会显示未发送电子邮件的成功。

================================

如果你可以用die()和echo语句捕获错误,那么你很好,否则请检查php.ini上的邮件配置。创建一个测试php页面,然后写:

mail('xxx@example.com','subject','content')

检查这是否有效。如果没有,您在服务器中遇到问题。

答案 1 :(得分:0)

你需要将php变量与字符串连接起来,而字符串的变量名称被解释为字符串的一部分。

检查process.php

mail( "preshesgirl@yahoo.com", $subject, $message, "From: ".$name." <".$email.">" );

它是:

mail( "preshesgirl@yahoo.com", $subject, $message, "From: $name <$email>" );