简体   繁体   中英

PHP email wont send variables

Basically the following code is an enquiry form that when the website user fills out, it sends the email to my email address. But something isn't working...

Here's the 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
}
?>

and here's the Html body:

<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>

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

it sends a basic email perfectly.

On all occasions the Enquiry Sent alert appears.

Anyone know why it wont work??

Run a var_dump(get_defined_vars()); and ensure that they are set. Also check /var/mail/user where user is the PHP or Apache user, and see what errors are thrown at more of an OS level.

1- Exactly, what do you get? An empty mail or no mail at all?

2- Try to debug your $_POST, like user1593858 said. Right after the php opening tag add this:

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

and see if your fields are correct.

3- you say that if you do:

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

it works; well try to substitute 'subject' with $subject or 'body_message' with $body_message until you isolate the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM