-2

I have an issue and I cannot figure out if its my PHP or server issue.

I created a contact form 'contact.php' its all on the same page, the HTML &PHP.

I Have checked the code over and over and cannot find any error. I do not receive any emails at the final destination email. Can anyone help me? This is the code I have

Here is my email code:

  $to = 'MY EMAIL ADDRESS GOES HERE';
    $subject = 'New Contact Submission on ';

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

 $body = <<<EMAIL

     Hi! My name is $name

     My Email is $email

     $message


     EMAIL;

        $header = "from: $email";

      if($_POST){
        if($name == '' || $email == '' || $message == ''){
            $feedback = 'Please Fill all areas';

        }else{

             mail($to, $subject, $body, $header);
         $feedback = 'Thank you! We will contact you soon!';

         }
     }


?>


Here is my HTML code for the contact form. Remember, I have this in 1 single 'contact.php' file.

                        <form action="" method="post">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="name" class="sr-only" >Name</label>
                                    <input placeholder="Name" id="name" type="text" class="form-control input-lg">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="email" class="sr-only"  >Email</label>
                                    <input placeholder="Email" id="email" type="text" class="form-control input-lg">
                                </div>
                            </div>

                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="message" class="sr-only"  >Message</label>
                                    <textarea placeholder="Message" id="message" class="form-control input-lg" rows="3"></textarea>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input type="submit" class="btn btn-primary btn-lg " action="" value="Send">
                                    <p id="feedback"><?php echo $feedback;?></p>
                                </div>
                            </div>


                        </form>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
Accu1
  • 11

2 Answers2

1

I noticed you had closed one of your if statement brackets twice and closed another one in the wrong place.

You also hadn't added a name to all of your HTML inputs.

This corrected code works...

PHP

<?php

    $to = 'Your email'; $subject = 'New Contact Submission on ';

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

    $body = "Hi! My name is $name

    My Email is $email

    $message";

    $header = "from: $email";

    if($_POST){
        if($name == '' || $email == '' || $message == ''){
            $feedback = 'Please Fill all areas';
        }
    }else{

       mail($to, $subject, $body, $header);
       $feedback = 'Thank you! We will contact you soon!';

    }

?>

HTML

<form action="" method="post">
    <div class="col-md-12">
        <div class="form-group">
            <label for="name" class="sr-only" >Name</label>
            <input placeholder="Name" id="name" type="text" class="form-control input-lg" name="name">
        </div>
    </div>
    <div class="col-md-12">
        <div class="form-group">
            <label for="email" class="sr-only"  >Email</label>
            <input placeholder="Email" id="email" type="text" class="form-control input-lg" name="email">
        </div>
    </div>

    <div class="col-md-12">
        <div class="form-group">
            <label for="message" class="sr-only"  >Message</label>
            <textarea placeholder="Message" id="message" class="form-control input-lg" rows="3" name="message"></textarea>
        </div>
    </div>
    <div class="col-md-12">
        <div class="form-group">
            <input type="submit" class="btn btn-primary btn-lg " action="" value="Send">
            <p id="feedback"><?php echo $feedback;?></p>
        </div>
    </div>
</form>
  • Hi Yudi! Thank you! I just tried this code and it seems to work, however, it does not send the email to its final destination, which is where I need the from to go. Any idea if its anything on the server or here? Thank you! – Accu1 Aug 18 '16 at 18:26
  • @Accu1 I tested on phpfiddle.org and the email sent to my email so I say it is a problem with your server. I would really appreciate if you would award with best answer as I am new and need the support, thank you! – Yudi Moszkowski Aug 18 '16 at 18:49
  • Hi, It does work, However, my server returns this error "SMTP error from remote mail server after end of data: 550 Messages should have one or no From headers, not 2." Any help? Thank you – Accu1 Aug 18 '16 at 22:26
0

I cant tell if its two single quotes or one double quote

if($_POST){ if($name == " || $email == " || $message == ")

is this incorrect? did you mean

if($_POST){ if($name == " " || $email == " " || $message == " ")
brad
  • 993
  • 7
  • 10
  • If you had a look at their code in edit mode (which is a total mess, I might add), you'll see they already declared the `$name = $_POST['name'];` and for the others before the conditionals. – Funk Forty Niner Aug 18 '16 at 16:50
  • another edit is pending review....thanks for the tip though – brad Aug 18 '16 at 16:52
  • I approved the edit, so we'll see what that looks like. *cheers* – Funk Forty Niner Aug 18 '16 at 16:54
  • Thanks for editing this! Its actually 2 separate single quotes, Brad. Do they have to be 2 double quotes? SO it currently is '_' not "" is this the error? Thanks – Accu1 Aug 18 '16 at 17:24