How to Send HTML Form Data to Email Using PHP

How to send form data to email using PHP
How to send form data to email using PHP

Share this Article!

Can I send HTML form data to email?

Yes, you can send your HTML Form Data to Email Using PHP. In this tutorial, I will show you How to Send HTML Form Data to Email Using PHP mail( ) function.

Moreover, this tutorial will explain How to send an HTML form to direct mail and at the same time, a confirmation email will be sent to the client on HTML form submission. A Simple PHP contact form to send emails with source code.

How to make an HTML form send data to an email address?

When a user fills out the contact form and presses submit button, it will send HTML form data to email using the PHP mailer function. Mostly, people want to get HTML form data to email after form submission.

How to Send Contact Form to Email Using PHP?

Follow these steps to send the HTML Form Data to Email Using PHP:

  1. Create a contact form using HTML and CSS
  2. Host HTML form on the live web hosting server to write PHP script
  3. Open your VS Code editor
  4. Connect VS Code editor to live web server using SFTP extension
  5. Change your file extension from .html to .php
  6. Write PHP code to send HTML form data to email using a PHP mailer

You can watch the step-by-step guide in the following video tutorial to send an email from the client side.

Video Tutorial – How to Send HTML Form Data to Email Using PHP

How to Send HTML Form Data to Email Using PHP

Now I am going to provide you with the source codes of HTML, CSS, and PHP of the contact form. I have created a separate sentMail.php file to write the PHP script. And add this PHP file with the HTML code of the contact form using the PHP include statement.

Important Note: First of all, host your contact form on any live web server in order to execute PHP code. And change your file extension to .php to run a PHP script.

If you don’t know how to connect your VS Code editor to a live web server. The SFTP extension is used for remote file editing in Visual Studio Code The following video tutorial will help you to link your Visual Studio Code Editor with the web hosting server.

How do I use SFTP in VSCode?

SFTP remote editing visual studio code

HTML Contact Form Source Code

Below is the HTML source code of the contact form to send the form details to an email address. This contact form contains the following user data fields.

  • User name
  • User email address
  • Phone number of the customer
  • Subject line for the email
  • A message field where customers can write a detailed explanation.
<?php include 'sentMail.php';?>

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <div class="container">  
    <form id="contact" action="" method="post">
      <h3>Quick Contact</h3>
      <h4>Contact us today, and get reply with in 24 hours!</h4>

        <input placeholder="Your name" name="name" type="text" tabindex="1" autofocus>
        <input placeholder="Your Email Address" name="email" type="email" tabindex="2">
        <input placeholder="Your Phone Number" name="tel" type="tel" tabindex="3" >
        <input placeholder="Type your subject line" type="text" name="subject" tabindex="4">
        <textarea name="message" placeholder="Type your Message Details Here..." tabindex="5"></textarea>
         <p class="success"> <?php echo $success;  ?></p>
         <p class="failed"> <?php echo $failed;  ?></p>
        <button type="submit" name="submit" id="contact-submit" data-submit="...Sending">Submit Now</button>

CSS Source Code of Contact Form

Following is the source code for CSS (Cascading Style Sheet).

@import url(,400,300,600);

* {

body {
	font-family:"Open Sans", Helvetica, Arial, sans-serif;
	font-size: 12px;
	background:rgb(3, 153, 212);

.container {
	margin:0 auto;

#contact input[type="text"], #contact input[type="email"], #contact input[type="tel"], #contact input[type="url"], #contact textarea, #contact button[type="submit"] { font:400 12px/16px "Open Sans", Helvetica, Arial, sans-serif; }

#contact {
	margin:50px 0;

#contact h3 {
	color: blue;
	display: block;
	font-size: 30px;
	font-weight: 700;

#contact h4 {
	margin:5px 0 15px;
	color: black;

fieldset {
	border: medium none !important;
	margin: 0 0 10px;
	min-width: 100%;
	padding: 0;
	width: 100%;

#contact input[type="text"], #contact input[type="email"], #contact input[type="tel"], #contact textarea {
	border:1px solid #CCC;
	margin:0 0 5px;

#contact input[type="text"]:hover, #contact input[type="email"]:hover, #contact input[type="tel"]:hover, #contact input[type="url"]:hover, #contact textarea:hover {
	-webkit-transition:border-color 0.3s ease-in-out;
	-moz-transition:border-color 0.3s ease-in-out;
	transition:border-color 0.3s ease-in-out;
	border:1px solid #AAA;

#contact textarea {

#contact button[type="submit"] {
	width: 100%;
	background:rgb(3, 153, 212);
	margin:0 0 5px;

#contact button[type="submit"]:hover {
	-webkit-transition:background 0.3s ease-in-out;
	-moz-transition:background 0.3s ease-in-out;
	transition:background-color 0.3s ease-in-out;

#contact button[type="submit"]:active { box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.5); }

#contact input:focus, #contact textarea:focus {
	border:1px solid #999;
::-webkit-input-placeholder {
:-moz-placeholder {
::-moz-placeholder {
:-ms-input-placeholder {

	color: green;
	font-weight: 700;
	padding: 5px;
	text-align: center;
	color: red;
	font-weight: 700;
	padding: 5px;
	text-align: center;

PHP Source Code – How to Send HTML Form to Direct Mail?

Following is the PHP Source code to get an email from an HTML form using PHP and send the HTML form to direct mail.


if(isset($_POST['submit'])) {
 $mailto = "[email protected]";  //My email address
 //getting customer data
 $name = $_POST['name']; //getting customer name
 $fromEmail = $_POST['email']; //getting customer email
 $phone = $_POST['tel']; //getting customer Phome number
 $subject = $_POST['subject']; //getting subject line from client
 $subject2 = "Confirmation: Message was submitted successfully | HMA WebDesign"; // For customer confirmation

 //Email body I will receive
 $message = "Cleint Name: " . $name . "\n"
 . "Phone Number: " . $phone . "\n\n"
 . "Client Message: " . "\n" . $_POST['message'];

 //Message for client confirmation
 $message2 = "Dear" . $name . "\n"
 . "Thank you for contacting us. We will get back to you shortly!" . "\n\n"
 . "You submitted the following message: " . "\n" . $_POST['message'] . "\n\n"
 . "Regards," . "\n" . "- HMA WebDesign";

 //Email headers
 $headers = "From: " . $fromEmail; // Client email, I will receive
 $headers2 = "From: " . $mailto; // This will receive client

 //PHP mailer function

  $result1 = mail($mailto, $subject, $message, $headers); // This email sent to My address
  $result2 = mail($fromEmail, $subject2, $message2, $headers2); //This confirmation email to client

  //Checking if Mails sent successfully

  if ($result1 && $result2) {
    $success = "Your Message was sent Successfully!";
  } else {
    $failed = "Sorry! Message was not sent, Try again Later.";



Final Words

The tutorial explained how do I send an email from the client side using the PHP mailer function. If you feel any difficulty understanding this post, you can ask in the comment section below. Your suggestion and queries are always welcome. If you find it helpful don’t forget to SUBSCRIBE to my YouTube Channel.

Share this Article!

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe Our YouTube Channel!

Follow for Latest Updates