Hey guys so in this chapter we gonna learn contact us form in PHP to send email of a form data to recipient so here we will learn everything from scratch will not skip any steps. 





Step 1

Create HTML form with some form fields data, this data will send to a mail to the recipient or admin. Create index.php file and put the following code.


index.php

<html>
    <head>
        <title>My Contact Form</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-3"></div>
                <div class="col-md-6">

                    <h3>My Contact form</h3>

                    <form action="send_mail.php" method="post" name="form1" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Name</label>
                            <input type="text" name="name" class="form-control">
                        </div>


                        <div class="form-group">
                            <label>Email</label>
                            <input name="email" type="text" class="form-control">
                        </div>
                        
                         <div class="form-group">
                            <label>Phone</label>
                            <input name="message" type="text" class="form-control">
                        </div>

                        <div class="form-group">
                            <label>Phone</label>
                            <textarea name="phone" cols="30" rows="4" class="form-control"></textarea>
                        </div>


                        <div class="form-group">
                            <label>Request Phone Call:</label><br>
                            Yes:<input type="checkbox" value="Yes" name="call">
                            No:<input type="checkbox" value="No" name="call">
                        </div>


                        <div class="form-group">
                            <label>Priority</label>
                            <select name="priority" size="1" class="form-control">
                                <option value="Low">Low</option>
                                <option value="Normal">Normal</option>
                                <option value="High">High</option>
                                <option value="Emergency">Emergency</option>
                            </select>
                        </div>


                        <div class="form-group">
                            <label>Type</label>
                            <select name="type" size="1" class="form-control">
                                <option value="update">Security</option>
                                <option value="change">Sells</option>
                                <option value="addition">Technical</option>
                                <option value="new">Order</option>
                            </select>
                        </div>



                        <div class="form-group">
                            <input type="submit" name="Submit" value="Send now" class="pull-right">
                        </div>

                    </form>

                </div>
                <div class="col-md-3"></div>


            </div>
        </div>

    </form>
</body>
</html>


Step 2

Now we need to create a send_mail.php file this file get a form data from index.php and send a mail


send_mail.php

<?php

/*
 * Some of the form fields 
 * that we need to get through post request and send mail using 
 * mail function 
 */

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];



$formcontent = " From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL@HERE.COM";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>


Note: the mail function only on your your live server, don't test on your localhost

Thanks, May this example help you.



 

Are you facing problems in understanding this article?

Download Source