0

I have a problem with my contact form that I have created. When the user clicks on the send email button, I receive the email with a header, but I can't see the user's input.

So basically I am able to see the header($subject) and the pre written text("This is an automated message"), but I am not able to see the contents of $email and $message. What could be wrong?

<?php

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

$to = "test@testemail.com"; 
$subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";

mail($to, $subject, $body); echo "Message Sent."; 
?>



<form id="contact-me-form" action="contact.php" name="contact_form "method="post">
    <input type="text" name="email" placeholder="Email Address">
    <textarea name="message" placeholder="Type Your Message Here"></textarea>
    <input id="sendEmail" type="submit" name="submit" value="Send">
</form>
Kevin Dunn
  • 3
  • 1
  • 2

2 Answers2

1

you need to check whether you have post parameters then send the email

change your code to

     <?php
    if(isset($_POST['email']) && isset($_POST['message'])){
        $email = $_POST['email']; 
        $message = $_POST['message'];

        $to = "test@testemail.com"; 
        $subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";

        mail($to, $subject, $body); echo "Message Sent."; 
    }
    ?>



    <form id="contact-me-form" action="contact.php" name="contact_form "method="post">
        <input type="text" name="email" placeholder="Email Address">
        <textarea name="message" placeholder="Type Your Message Here"></textarea>
        <input id="sendEmail" type="submit" name="submit" value="Send">
    </form>
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77
0

Try like this

<?php

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

$to = "test@testemail.com"; 
$subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";
$body= $_POST["message"]
if ($_SERVER["REQUEST_METHOD"] == "POST") {
mail($to, $subject, $body); echo "Message Sent."; 

}
?>



<form id="contact-me-form" action="contact.php" name="contact_form "method="post">
    <input type="text" name="email" placeholder="Email Address">
    <textarea name="message" placeholder="Type Your Message Here"></textarea>
    <input id="sendEmail" type="submit" name="submit" value="Send">
</form>

This will send an email to test@testemail.com when the user submit a message in the form

O.Rares
  • 1,031
  • 1
  • 16
  • 19