Contact Form PHP

A PHP Contact Form is a tool we can say is used for contacting people generally it is used at the end of the websites for taking reviews for example if somebody visits your website and they want to get you so through a contact form they can easily approach you.

In a contact form, a user can ask questions and give reviews and all this information by email. Let’s see how it works.

PHP Contact Form Example


<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Contact Form</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="">
  <style type="text/css">
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;

  min-height: 100vh;
  background: url(background.jpg) no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  color: #fff;
  max-width: 500px;
  line-height: 65px;
  padding-left: 50px;
  font-size: 18px;

.contact-info i{
  margin-right: 20px;
  font-size: 25px;

  max-width: 700px;
  margin-right: 50px;

.contact-info, .contact-form{
  flex: 1;

.contact-form h2{
  color: black;
  text-align: center;
  font-size: 35px;
  text-transform: uppercase;
  margin-bottom: 30px;

.contact-form .text-box{
  background: #000;
  color: #fff;
  border: none;
  width: calc(55% - 10px);
  height: 50px;
  padding: 12px;
  font-size: 15px;
  border-radius: 5px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  opacity: 0.9;

.contact-form .text-box:first-child{
  margin-right: 15px;

.contact-form textarea{
  background: #000;
  color: #fff;
  border: none;
  width: 100%;
  padding: 12px;
  font-size: 15px;
  min-height: 200px;
  max-height: 400px;
  resize: vertical;
  border-radius: 5px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  opacity: 0.9;

.contact-form .send-btn{
  float: right;
  background: #2E94E3;
  color: #fff;
  border: none;
  width: 120px;
  height: 40px;
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 2px;
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
  transition-property: background;

.contact-form .send-btn:hover{
  background: #0582E3;

@media screen and (max-width: 950px){
    flex-direction: column;
  .contact-info, .contact-form{
    margin: 30px 50px;

  .contact-form h2{
    font-size: 30px;

  .contact-form .text-box{
    width: 100%;

/*css for alert messages*/

    <!--alert messages start-->
    <!--alert messages end-->

    <!--contact section start-->
    <div class="contact-section">
      <div class="contact-info">
      <div class="contact-form">
        <h2>Contact Us</h2>
        <form class="contact" action="mail.php" method="post">
          <input type="text" name="name" class="text-box" placeholder="Your Name" required>
          <input type="email" name="email" class="text-box" placeholder="Your Email" required>
         <input type="text" name="subject" class="text-box" placeholder="subject" required>

          <textarea name="message" rows="5" placeholder="Your Message" required></textarea>
          <input type="submit" name="sub" class="send-btn" value="Send">
    <!--contact section end-->


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

if (isset($_POST['sub'])) {

  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
               # code...


//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP


//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
//Set the hostname of the mail server
$mail->Host = '';
// use
// $mail->Host = gethostbyname('');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = '[email protected]';
//Password to use for SMTP authentication
$mail->Password = 'yourmailpasswordl';
//Set who the message is to be sent from
$mail->setFrom('[email protected]');
//Set who the message is to be sent to
$mail->addAddress('[email protected]');

//Set the subject line
$mail->Subject = $subject;
//$mail->body = $message;
//Read an HTML message body
//Attach an image file (optional)
//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';

Above is the code for the simple responsive contact form. In the above code we create a file named Index.php we create a simple HTML contact form and then we design it with CSS. In the contact form, we took 4 input fields name, email, subject, and message, and create a button for submitting the form.

Then we create another PHP file named Mail.php in which we did the backend work. In Mail.php we use a very simple and easy-to-use method called PHPMailer for sending emails through the localhost Xamp server. You just have to do a few adjustments and then your code is ready to compile.

How to Setup PHPMailer

  • First of all, open your browser and search for PHPMailer 
  • Then download the PHPMailer library zip file.
  • After downloading, extract the file in Xampp htdocs folder where you keep your other PHP projects.
  • Now open the PHPMailer folder and go to the src file and copy all files from the src file.
  • After copying all files paste them into that folder where you will create your contact form project.
  • Then open the Xamp server and start Apache and then past the following code in your project file:

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
    $mail->isSMTP(); //Send using SMTP
    $mail->Host = ''; //Set the SMTP server to send through
    $mail->SMTPAuth = true; //Enable SMTP authentication
    $mail->Username = '[email protected]'; //SMTP username
    $mail->Password = 'secret'; //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
    $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User'); //Add a recipient
    $mail->addAddress('[email protected]om'); //Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name

    $mail->isHTML(true); //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

In the above code just add your dummy email in username and password and don’t forget to on your Less Secure App Access in your Gmail Account, If you don’t allow the access you will not able to receive emails from the local server.


In this tutorial we talk about PHP Contact Form we discuss a simple definition and then we talk discuss an example of PHP Contact Form using the PHPMailer function. We discuss how to set up PHPMailer on your device.

Suggested Article:

Leave a Comment