简体   繁体   中英

PHP Contact Form Not Being Received In Email

I know there is a few of these posts already up, and I read through them, but was unable to find the solution to my problem. The PHP and HTML looks good so I am not exactly sure why I am not receiving any email from the submitted contact form.

Here is the PHP:

<?php

$Fname = $_POST ['Fname'];
$Lname = $_POST ['Lname'];
$email = $_POST ['email'];
$message = $_POST ['message'];

$to = 'myName@mywebsite.com';
$subject = 'Contact From My Website';
$msg = " First Name: $Fname/n" .
"Last Name: $Lname/n" .
"Email: $email/n" .
"Message: $message";

mail ($to, $subject, $msg,"From: " . $Fname . $Lname);

$confirm = "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";

?>

And here is the HTML form code:

<form id="form1" name="form1" method="post" action="send.php">
                <tr>
                    <label><td width="160px" class="labels">First Name:&nbsp;&nbsp;</td>
                        <td class="input"><input type="text" name="Fname" id="Fname"/></td>
                    </label>
                </tr>
                <tr>
                    <label><td class="labels">Last Name:&nbsp;&nbsp;</td>
                        <td class="input"><input type="text" name="Lname" id="Lname"/></td>
                    </label>
                </tr>
                <tr>    
                    <label><td class="labels">Email:&nbsp;&nbsp;</td>
                        <td class="input"><input type="email" name="email" id="email"/></td>
                    </label>
                </tr>
                <tr>    
                    <label><td class="labels">Message:&nbsp;&nbsp;</td>
                        <td class="input"><textarea name="message" id="message" cols="30" rows="5"></textarea></td>
                    </label>                                    
                </tr>
                <tr>                        
                    <td class="labels"><input type="submit" name="submit" id="submit" value="Submit"/></td>
                </tr>
            </form> 

Any help would be greatly apreciated

EDIT:

There were many errors.

Improper use of \\n , mail() headers and trying to echo a success message at the end.

Here is a tested and working mail handler script.

NOTE : I added an if{isset condition.

Change email@example.com with your own E-mail address.

<?php

if(isset($_POST['submit']))

$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = "email@example.com"; // <<< change this to your own E-mail address
$subject = "Contact From My Website";
$msg = "First Name: $Fname\n" . "Last Name: $Lname\n" . "Email: $email\n" . "Message: $message";

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

mail($to, $subject, $msg, $headers);

echo "Thank you for contacting us! Please allow 48 hours for a representative to respond. Click <a href='contact.php'>here</a> to return to the previous page.";

?>


Supplement

Please read up on the mail( ) function and its proper use of headers by visiting:

There are plenty of examples on that page.

One major error is your use of /n and all need to be changed to \\n if anything.

That alone will ultimately make your handler fail .

"From" should not include the name, instead it should include the from email address.

So, instead of

"From: " . $Fname . $Lname

You should be doing

"From: ". $myEmail

Here's a valid header from the php manual :

$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

I guess Puggan and Fred get credit for this answer as-well, since they have posted out the obvious but not yet made it an answer.

You should also make sure that the id of the message labels match to $_POST['msg'], so you should not be using 'msg' in one place and 'message' in another.

FYI: skip the spaces ' ' between the function name and the parameter arguments. ie

$_POST ['stuff'];

Add

$headers = "From:" . $Fname . $Lname;

and use this:

mail ($to, $subject, $msg, $headers);

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