PHP电子邮件不会发送变量

时间:2012-08-25 20:41:12

标签: php html email

以下代码基本上是一个查询表单,当网站用户填写时,它会将电子邮件发送到我的电子邮件地址。但有些东西不起作用......

这是php:

<?php
$field_name = $_POST['efName'];
$field_email = $_POST['efEmail'];
$field_phone = $_POST['efPhone'];
$field_date = $_POST['efTravelDate'];
$field_duration = $_POST['efLengthOfStay'];
$field_otherInfo = $_POST['efOtherInfo'];

$mail_to = 'James_Burnside94@hotmail.com';
$subject = 'Enquiry Applicant-'.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Date: '.$field_date."\n";
$body_message .= 'Duration: '.$field_duration."\n";
$body_message .= 'otherInfo: '.$field_otherInfo."\n";

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Enquiry Sent.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Enquiry form failed to send. \n Please try again, or send an email             to enquiry@cannes-rental-apartment.com');
        window.location = 'index.html';
    </script>
<?php
}
?>

这是Html的主体:

<body>


<fieldset style="display:block; width:500px;">
    <form action="enquiry.php" method="post">
    <table> 
                <tr> <td> <span class="enquiryFormHeader"> Enquiry Form     </span> <p>         </p></td> </tr>
            <tr> <td> Name: </td> <td> <input type="text" name="efName"     id="efName"> </td> </tr>
            <tr> <td> Email: </td> <td> <input type="text" name="efEmail"     id="efEmail"> </td> </tr>
            <tr> <td> Phone or Moblie: </td> <td> <input type="text"     name="efPhone" id="efPhone"> </td> </tr>
            <tr> <td> Intended travel date: </td> <td> <input type="text"     name="efTravelDate" id="efTravelDate"> </td> </tr>
            <tr> <td> Intended length of stay: </td> <td> <input type="text"     name="efLengthOfStay" id="efLengthOfStay"> </td> </tr>
            <tr> <td valign="top"> Any other information:  </td> <td> <textarea     name="efOtherInfo" id="efOtherInfo"> </textarea> <p> </p> </td> </tr>
            <tr> <td><input type="submit" value="Send Form"     class="submitButton"> </td> </tr>
    </table>

    </form>
    </fieldset>


</body>

如果我这样做     $ mail_status = mail($ mail_to,'subject','body_message','headers');

它完美地发送了一封基本邮件。

在所有情况下都会显示“已发送查询”警报。

任何人都知道它为什么不起作用?

2 个答案:

答案 0 :(得分:1)

运行var_dump(get_defined_vars());并确保已设置。同时检查/var/mail/user其中user是PHP用户还是Apache用户,并查看更多操作系统级别引发的错误。

答案 1 :(得分:0)

1-确切地说,你得到了什么?一封空邮件或根本没邮件?

2-尝试调试你的$ _POST,就像user1593858说的那样。在php开启标记之后添加:

echo "<pre>";
print_r($_POST);
echo "</pre>";

并查看您的字段是否正确。

3-你说如果你这样做:

$mail_status = mail($mail_to, 'subject', 'body_message', 'headers');

它有效;试着用$subject或'body_message'替换'$body_message替换'subject',直到你找出问题为止。