PHP表单到电子邮件[mail()函数]不起作用

时间:2013-11-22 16:52:01

标签: php forms

我的联系表格有问题。当我点击提交按钮时,php页面会加载,但不会显示php消息,也不会发送电子邮件。

这是我的表单代码:

<form name="Contact" method="post" action="form_to_email.php">
    <table width="100%" border="0" cellpadding="0" cellspacing="20">
      <tr>
        <td width="25%"><em>Full name:</em></td>
        <td width="75%"><input type="text" name="FName" id="FName" accesskey="F" placeholder="First name" required autocomplete="on">
        <input type="text" name="LName" id="LName" accesskey="L" placeholder="Last name" required autocomplete="on"></td>
      </tr>
      <tr>
        <td><em>E-mail address:</em></td>
        <td><input name="Email" type="text" id="Email" placeholder="you@you.com" accesskey="E" size="25" required autocomplete="on"></td>
      </tr>
      <tr>
        <td><em>Subject:</em></td>
        <td><select name="Subject" id="Subject" accesskey="S">
          <option value="Comment">Comment</option>
          <option value="Question">Question</option>
        </select></td>
      </tr>
      <tr>
        <td><em>Comments/Questions:</em></td>
        <td><textarea name="Comments" id="Comments" cols="45" rows="5" accesskey="C" required title="Please type your comment or question here."></textarea></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="Submit" id="Submit" value="Submit"></td>
      </tr>
    </table>
</form>

PHP代码(form_to_email.php)是:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "*****@hotmail.com";
$email_subject = "Form Mail";


function died($error) {
    // your error code can go here
    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();
}

// validation expected data exists
if(!isset($_POST['FName']) ||
    !isset($_POST['LName']) ||
    !isset($_POST['Email_from']) ||
    !isset($_POST['Subject']) ||
    !isset($_POST['Comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$fname = $_POST['FName']; // required
$lname = $_POST['LName']; // required
$email_from = $_POST['Email_from']; // required
$subject = $_POST['Subject']; // not required
$comments = $_POST['Comments']; // required

$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,$fname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$lname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 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($fname)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

我正在使用的服务器可能有问题吗?默认的PHP变量在这里http://www.freewebhostingarea.com/phpinfo-default_variables.html,除了我不明白该页面的内容......

同样通过成员区域,我可以配置一些PHP变量。它表示对于新用户,safe_mode会在最初的几个小时内自动关闭。其他默认值为:register_globals为ON,magic_quotes_gpc为OFF,allow_url_include为OFF,short_open_tag为OFF。

1 个答案:

答案 0 :(得分:0)

在您的表单中,您使用了名称Email输入

<input name="Email" type="text" id="Email" placeholder="you@you.com" accesskey="E" size="25" required autocomplete="on">

但在form_to_email.php中,您使用了以下

 if(isset($_POST['email'])) {

 }

所以它应该是,

 if(isset($_POST['Email'])) {

 }

以及$email_from = $_POST['Email_from'];也应更改为$email_from = $_POST['Email'];