Search code examples
phpemailgmailcontact-form

Contact form - user's name and email coming through as 'unknown'


Apologies, I am aware this question has been asked quite afew times already on this site, however I have tried to implement what others have suggested but with no luck.

Same as everyone else I have a very simple contact form and for the sake of this example I have stripped the PHP coding to the bare minimum so it is simpler to look at.

It works fine in the sense I receive the email in GMail with the message, however the email and name the user filled-in never shows up.

  • I have tried taking '\r' out and just leaving '\n\
  • Other methods have either resulted in the email going to junk or the same (coming through as unknown).

      <form action="contact.php" method="post">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="email">Email address</label>
                                        <input type="email" class="form-control" id="email" placeholder="Enter email">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="name">Name</label>
                                        <input type="text" class="form-control" id="name" placeholder="Name">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="message">Message</label>
                                <textarea name="message" id="message" class="form-control" rows="3"></textarea>
                            </div>
                            <button type="submit" class="btn tf-btn btn-default">Submit</button>
                        </form>
    

PHP Code

<?php

$to = '[email protected]';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = 'RE: RMW interest';

$headers .= 'From:' .$name. " ".'<'.$email.'>'."\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";

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

echo "Thank you for contacting us, we will respond shortly!";

?>

I would be most grateful if someone could shed some light on this particular case please.

Many thanks


Solution

  • The problem in HTML code not in PHP,

    You forgot to name the inputs, so when you call print_r() in contact.php file you will found just the message.

    in contact.php when you write print_r($_POST); the output will be

    Array ( [message] => message) 
    

    Now you know the problem so you want to name all inputs.

    <input name="email" type="email" class="form-control" id="email" placeholder="Enter email">
    ...
    <input name="name" type="text" class="form-control" id="name" placeholder="Name">
    ...
    <button name="submit" type="submit" class="btn tf-btn btn-default">Submit</button>
    

    So the output now is

    Array ( [email] => [email protected] [name] => test [message] => test message [submit] => ) 
    

    And here i named the button because you can check if the button clicked send the message.

    if(isset($_POST['submit'])){
        //your code here
    }
    

    hope this helps