In this tutorial I'll teach you to contact us form in PHP this form will send a mail to the user and admin as well when user type subject email and query means message the subject and message will get a recipient to user whatever they will type so here I am using PHPMailer for sending mails.

Install PHPMailer

composer require phpmailer/phpmailer

Project structure 


use PHPMailer\PHPMailer\PHPMailer;
<!DOCTYPE html>
        <title>Contact Form</title>
        <link href="" rel="stylesheet">
                margin: 20px 0;
                background: #ccc;

                background: #fff;
                padding: 25px;

        <div class="container">
            <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-4">

                    <form action="" method="post">
                        <h3>Contact form</h3>
                        if (isset($_POST['email'])) {

                            $email = $_POST['email'];
                            $subject = $_POST['subject'];
                            $query = $_POST['query'];

                            $mail = new PHPMailer();
                            $mail->Host = ""; // Enter your host here
                            $mail->SMTPAuth = true;
                            $mail->Username = ""; // Enter your email here
                            $mail->Password = ""; //Enter your passwrod here
                            $mail->Port = 587;
                            $mail->From = "";
                            $mail->FromName = "From Name";

                            $mail->Subject = $subject;

                            $message = file_get_contents('templete.php');
                            $message = str_replace('%subject%', $subject, $message);
                            $message = str_replace('%message%', $query, $message);
                            $mail->AddAddress(''); //admin email
                            if (!$mail->Send()) {
                                echo "Mailer Error: " . $mail->ErrorInfo;
                            } else {
                        <div class="form-group">
                            <label for="subject">Subject:</label> 
                            <input type="text" name="subject" id="subject" maxlength="255" class="form-control">

                        <div class="form-group">
                            <label for="email">Your email address:</label> 
                            <input type="email" name="email" id="email" maxlength="255" class="form-control">

                        <div class="form-group">
                            <label for="query">Your question:</label>
                            <textarea cols="30" rows="8" name="query" id="query" placeholder="Your question" class="form-control"></textarea>

                        <div class="form-group">
                            <input type="submit" value="Submit">
                <div class="col-md-4"></div>



    <p> Thank you for contacting us we will get backto you soon. your details are as follows:</p>
        Subject: %subject% <br>
        Message: %message%<br>


<h2>Thank you for contacting us</h2>

Run the following code and see the result output

Download source code

Are you facing problems in understanding this article? download source code now