How To ADD Simple PHP Contact Form With Email

How To ADD Simple PHP Contact Form With Email

How To ADD Simple PHP Contact Form With Email

0 Sales


For almost every website, the contact form is an essential element. The feedback form allows tourists to contact the website operator. Visitors can easily make their questions, thoughts, opinions and feedback to the website director on the company, service or item using the contact form. The details submitted may also be sent via email to the website owners or managers.


The contact form lets you accept visitors ' inquiries and provides visitors with a quick answer. The idea of a contact form is very simple and you can send your inquiry to your organizations via email.


In this tutorial we show you how to create and integrate a simple contact form with PHP. You can add a contact form to your website within 5 minutes via our PHP contact form script. Not only is the contact form sent, but every form submitted using PHP can also send you an e-mail.


We will split the PHP contact form script into two parts, HTML and PHP, to help you better understand it. This software can be put together on the web page where the contact form will be shown.


Contact Form HTML


A contact form (name, e-mail, topic and message) and a submit button will be shown in the following HTML. In the web page to show a contact form, enter this entire code.


<h2>Simple PHP Contact Form</h2>

<div class="contactFrm">

    <?php if(!empty($statusMsg)){ ?>

        <p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>

    <?php } ?>

    <form action="" method="post">


        <input type="text" name="name" placeholder="Your Name" required="">

        <h4>Email </h4>

        <input type="email" name="email" placeholder="[email protected]" required="">


        <input type="text" name="subject" placeholder="Write subject" required="">


        <textarea name="message" placeholder="Write your message here" required=""> </textarea>

        <input type="submit" name="submit" value="Submit">

        <div class="clear"> </div>




PHP (Contact form submission script)


We will get the submitted data using $POST, validate the data, validate the FILTER VALIDATE EMAIL email address, and send a PHP Mail() email to the specified recipient.


Replace the email address of the sender with the email you want and insert the next PHP code in the website in front of the contact form.



$statusMsg = '';

$msgClass = '';


    // Get the submitted form data

    $email = $_POST['email'];

    $name = $_POST['name'];

    $subject = $_POST['subject'];

    $message = $_POST['message'];


    // Check whether submitted data is not empty

    if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){


        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){

            $statusMsg = 'Please enter your valid email.';

            $msgClass = 'errordiv';


            // Recipient email

            $toEmail = '[email protected]';

            $emailSubject = 'Contact Request Submitted by '.$name;

            $htmlContent = '<h2>Contact Request Submitted</h2>






            // Set content-type header for sending HTML email

            $headers = "MIME-Version: 1.0" . "\r\n";

            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


            // Additional headers

            $headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";


            // Send email


                $statusMsg = 'Your contact request has been submitted successfully !';

                $msgClass = 'succdiv';


                $statusMsg = 'Your contact request submission failed, please try again.';

                $msgClass = 'errordiv';




        $statusMsg = 'Please fill all the fields.';

        $msgClass = 'errordiv';





CSS (Styling contact form)

To style our example please contact us by using the following CSS.


.contactFrm h4 {

    font-size: 1em;

    color: #252525;

    margin-bottom: 0.5em;

    font-weight: 300;

    letter-spacing: 5px;


.contactFrm input[type="text"], .contactFrm input[type="email"] {

    width: 92%;

    color: #9370DB;

    background: #fff;

    outline: none;

    font-size: 0.9em;

    padding: .7em 1em;

    border: 1px solid #9370DB;

    -webkit-appearance: none;

    display: block;

    margin-bottom: 1.2em;


.contactFrm textarea {

    resize: none;

    width: 93.5%;

    background: #fff;

    color: #9370DB;

    font-size: 0.9em;

    outline: none;

    padding: .6em 1em;

    border: 1px solid #9370DB;

    min-height: 10em;

    -webkit-appearance: none;


.contactFrm input[type="submit"] {

    outline: none;

    color: #FFFFFF;

    padding: 0.5em 0;

    font-size: 1em;

    margin: 1em 0 0 0;

    -webkit-appearance: none;

    background: #9370DB;

    transition: 0.5s all;

    border: 2px solid #795CB4;

    -webkit-transition: 0.5s all;

    transition: 0.5s all;

    -moz-transition: 0.5s all;

    width: 47%;

    cursor: pointer;


.contactFrm input[type="submit"]:hover {

    background: none;

    color: #9370DB;



p.succdiv{color: #008000;}



You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : May 14, 2020

Updated Date : May 14, 2020


Comments : 0

Downloads : 0