1

I have an html form where users will submit their contact information for us to get in contact with them. I would like to have all of this live on index.html, but I am not opposed to having to create another file in the folder.

<form class="row" method="post" action="contact.php">
    <div class="col-md-12">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Your Full Name">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Phone Number*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Email Address*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Company Name*">
        </div>
        <div class="form-group">
            <textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
        </div>
    </div>

    <div class="buttn_outer">
        <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left;">Submit</button>
    </div>
</form>

I have had php installed on the server machine. Looking at other examples I have found attempted using a php script as follows.

<?php

if($_POST["submit"]) {
    $recipient = "me@me.com";
    $subject = "Some Message subject";
    $sender = $_POST["sender"];
    $senderEmail = $_POST["senderEmail"];
    $message = $_POST["message"];

    $mailBody = "Name: ".$sender."\nEmail: ".$senderEmail."\n\n".$message;

    mail($recipient, $subject, $mailBody, "From: ".$sender." <".$senderemail.">");
    $thankYou = "<p>Thank you! Your message has been sent.</p>";
}
?>

As of right now it goes to the success message however I never get anything to go to my email. I am extremely new to php, any help/advice is appreciated.

Lance
  • 4,736
  • 16
  • 53
  • 90
samiracle
  • 25
  • 5

3 Answers3

2

None of your input fields have name attributes.

<input type="text" class="form-control" placeholder="Email Address*">
<input type="text" class="form-control" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>

Should be

<input type="text" class="form-control" name="senderEmail" placeholder="Email Address*">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" name="message" placeholder="Questions / comments"></textarea>

Basically, the name attribute of the input field should match what the $_POST key is.

So, <input name="name" /> would be handled as $_POST['name']

Also, it looks like you're mixing up the $recipient and $senderEmail variables. PHP's mail function specifies that the first parameter is the recipient. I understand that me@me.com is clearly not a real email. But, the recipient should be the user's email that he/she has submitted in the form. Make sure the mail function works by testing it without submitting the form and if it does, then try to make it work with the form variables.

Lance
  • 4,736
  • 16
  • 53
  • 90
0

Other things, I'd throw in

($_POST["submit"]) should be changed to

!empty($_POST["submit"]) - or - isset($_POST["submit"])

Never assume a variable is set - don't run any tests on it or use it's value until you check to see if it's been set (or you initialize it yourself).

Depending on your PHP config, that line can fail. So if you move your contact.php page to another server, and then it may crash with errors.

As Lance mentioned, every form field that you want to pass back to your server needs a name attribute. This is what links it to the $_POST array when it gets passed to the server.

Also, you can use the users email as the from address. However, your email will likely get flagged as spam when the receiving server sees that it's not originated from that user's domain email server. You'll have to play around with your server's email settings so you authenticate it as a legitimate from address if you're trying to "spoof" it (aka not use the server's default).

If you want to override the default from address (or reply to address), you need to do it in the header fields (you can google this one to see you options).

If you want to include HTML tags or elements in your HTML, you'll also need to indicate this in the headers parameter of the mail function. For simply alert emails, like what you're doing - I just use plain text and use carriage returns (like you have with \n's) and don't use items like <p></p>.

airtech
  • 486
  • 3
  • 12
0

Try This May Help You...

index.html  

 <form class="row" method="post" action="contact.php">
        <div class="col-md-12">
            <div class="form-group">
                <input type="text" class="form-control" name="sender" placeholder="Your Full Name">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="phno" placeholder="Phone Number*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="email" placeholder="Email Address*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control"  name="cmp" placeholder="Company Name*">
            </div>
            <div class="form-group">
                <textarea class="form-control lg_textarea" name="cmt" placeholder="Questions / comments"></textarea>
            </div>
        </div>

        <div class="buttn_outer">
            <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left";>Submit</button>
        </div>
    </form>

    contact.php

    <?php
        $to='me@me.com';
        $header = "From: info@".$_SERVER["SERVER_NAME"]."\n";
        $header .= "Content-Type: text/html; charset=iso-8859-1\n";

        $sender=$_POST['sender'];
        $email=$_POST['email'];
        $cmt=$_POST['cmt'];
        $cmp=$_POST['cmp'];
        $phno=$_POST['phno'];

        $subject='Message Subject';

        $body='<table width="90%" border="0">
        <tr>
        <td><b>Sender:</b></td> <td>'.$sender.'</td>
        </tr>
        <tr>
        <td><b>Email:</b></td> <td>'.$email.'</td>
        </tr>
        <tr>
        <td><b>Phone No:</b></td> <td>'.$phno.'</td>
        </tr>
        <tr>
        <td><b>Copmany Name:</b></td> <td>'.$cmp.'</td>
        </tr>
        <tr>
        <td><b>Comment:</b></td> <td>'.$cmt.'</td>
        </tr>
        <tr></table>';



        mail($to,$subject,$body,$header);
       header('location:index.html');



    ?>
Edwin Thomas
  • 1,186
  • 2
  • 18
  • 31