Contact Form PHP



 
 
Send Mail By HTML Form With PHP
Create a page name contact.html
 
 
<form action="mail.php" method="POST">
<div class="form-group row">
<label for="example-text-input" class="col-xs-2 col-form-label">Name</label>
<div class="col-xs-10">
<input class="form-control brradius" type="text" name="fname" value="" id="example-text-input">
</div>
</div>
<div class="form-group row">
<label for="example-email-input" class="col-xs-2 col-form-label">Email</label>
<div class="col-xs-10">
<input class="form-control brradius" type="email" name="email" value="" id="example-email-input">
</div>
</div> 
 
<div class="form-group row">
<label for="example-text-input" class="col-xs-2 col-form-label">Phone</label>
<div class="col-xs-10">
<input class="form-control brradius" type="number" name="phone" value="" id="example-text-input">
</div>
</div>
<div class="form-group row">
<label for="comment" class="col-xs-2">Message:</label>
<div class="col-xs-10">
<textarea class="form-control brradius" name="message" value="" rows="5" id="comment"></textarea>
</div>
</div>
<button type="submit" name="submit" value="" class="btn btn-primary pull-right" style="background:#2d7fc7; border-radius:0; border:none;">Submit</button>
</form>
 
 
 
Create a page name mail.php
<?php
 
if(isset($_POST['submit']))
    {
        $name = $_POST['fname']; // Get Name value from HTML Form
        $email_id = $_POST['email']; // Get Email Value
        $mobile_no = $_POST['phone']; // Get Mobile No
        $msg = $_POST['message']; // Get Message Value
        
        $to = "info@xyz.com"; // You can change here your Email
        $subject = "'$name' has been sent a mail"; // This is your subject
        
        // HTML Message Starts here
        $message ="
        <html>
            <body>
                <table style='width:600px;'>
                    <tbody>
                        <tr>
                            <td style='width:150px'><strong>Name: </strong></td>
                            <td style='width:400px'>$name</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>Email ID: </strong></td>
                            <td style='width:400px'>$email_id</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>Mobile No: </strong></td>
                            <td style='width:400px'>$mobile_no</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>Message: </strong></td>
                            <td style='width:400px'>$msg</td>
                        </tr>
                    </tbody>
                </table>
            </body>
        </html>
        ";
        // HTML Message Ends here
        
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
 
        // More headers
        $headers .= 'From: Admin <mail@xyz.com>' . "\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
        $headers .= 'Cc: info@xyz.com' . "\r\n"; // If you want add cc
        $headers .= 'Bcc: mail@xyz.com' . "\r\n"; // If you want add Bcc
        
        if(mail($to,$subject,$message,$headers)){
            // Message if mail has been sent
            echo "<script>
                    alert('Mail has been sent Successfully.');
                </script>";
        }
        else{
            // Message if mail has been not sent
            echo "<script>
                    alert('EMAIL FAILED');
                </script>";
        }
    }
?>
 
Now Every Thing is done and form is ready for use. thank you