php code to send email from contact form 2018

In our contact form serial, we will give you detailed instructions about developing PHP code to send email from contact form.

Your contact from will look like the screenshot

php code to send email from contact form


In this post, we will create a contact form with the similar layout. The purpose of this tutorial is making the PHP script sent email from web form and sending email function work correctly. If you want to change the contact form layout, you can put your style code into the style.css file in the style folder.

In order to create a contact form, we will need those file types:

  • HTML file (.html): HTML source codes are used for creating contact page layout such as displaying textbox, submit button, etc.
  • CSS file (.css): CSS codes are used for managing box position, font, font color, and so on.
  • PHP file (.php): PHP codes are used for data processing

Now, let’s get started to develop code send mail php for a simple contact form. One contact form usually includes these fields:

  • Username: A visitor needs to fill in his/her name in this field
  • Email: This field represents visitor’s email address
  • Subject: This field represents the email’s subject
  • Message: This field represents the content of the email
  • Submit button

These fields are necessary to create a simple php email form. You can add other fields to your contact form later.

If your website uses Prestashop platform, I strongly recommend that you should use the Form Builder module to create your unique contact form. You can check the module here:

Step 1: Create HTML contact form: contactform.html

<div class="contact-wapper">
 <h2 class="title"><strong>Contact</strong> Us</h2>
 <form id="contactForm" action="php/contact-form.php" method="POST" >
 <div class="row">
 <div class="form-group">
 <div class="col-md-6">
 <label>Your name *</label>
 <input type="text" value="" maxlength="100" class="form-control" name="name" id="name" required="" aria-required="true">
 <div class="col-md-6">
 <label>Your email address *</label>
 <input type="email" value="" maxlength="100" class="form-control" name="email" id="email" required="" aria-required="true">
 <div class="row">
 <div class="form-group">
 <div class="col-md-12">
 <input type="text" value="" maxlength="100" class="form-control" name="subject" id="subject" required="" aria-required="true">
 <div class="row">
 <div class="form-group">
 <div class="col-md-12">
 <label>Message *</label>
 <textarea maxlength="5000" rows="10" class="form-control" name="message" id="message" required="" aria-required="true"></textarea>
 <div class="row">
 <div class="col-md-12">
 <input type="submit" value="Send Message" class="btn btn-primary btn-lg mb-xlg" />

Step 2: Style CSS for contact form: style.css

In the project folder, you have to create a new  styles folder. In this new folder, create a CSS file: style.css. Source code:

*{box-sizing: border-box;}

.col-md-6 {

width: 50%;

float: left;

padding: 0 15px;


.col-md-12{width:100%;float:left;padding:0 15px}

label {

display: inline-block;

max-width: 100%;

margin-bottom: 8px;

font-weight: bold;


.form-control {

display: block;

width: 100%;

height: 34px;

padding: 6px 12px;

font-size: 14px;

line-height: 1.42857143;

color: #555;

background-color: #fff;

background-image: none;

border: 1px solid #ccc;

border-radius: 4px;

-webkit-box-shadow: inset 0 1px 1pxrgba(0, 0, 0, .075);

box-shadow: inset 0 1px 1pxrgba(0, 0, 0, .075);

-webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;

-o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;

transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;


.form-group {

width: 100%;

float: left;

padding: 15px 0;


.contact-wapper {

width: 40%;

margin: auto;


h2.title {

padding: 0 15px;


textarea#message {

height: auto;


input.btn.btn-primary.btn-lg.mb-xlg {

color: #fff;

background-color: #204d74;

border-color: #122b40;

height: 30px;

border: none;


Go back to the contactform.html file, please put these codes inside the <header></header> tag:

<link type=”text/css” rel=”stylesheet” href=”style/style.css” />

Step 3: Write php code to send email from html contact from

Source code:



header('Expires: ' . gmdate('r', 0));

header('Content-type: application/json');

$to = '';

$subject = $_POST['subject'];

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

$name = $_POST['name'];

$email = $_POST['email'];

$fields = array(

0 =>array(

'text' => 'Name',

'val' => $_POST['name']


1 =>array(

'text' => 'Email address',

'val' => $_POST['email']


2 =>array(

'text' => 'Message',

'val' => $_POST['message']



$message = "";

foreach($fields as $field) {

$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";


$headers = '';

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

$headers .= "Reply-To: " .  $email . "\r\n";

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

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

if (mail($to, $subject, $message, $headers)){

$arrResult = array ('response'=>'success');

} else{

$arrResult = array ('response'=>'error');



} else {

$arrResult = array ('response'=>'error');




In order to change the received email address, you need to replace to the expected email address in this code line: $to = ‘’.

All are done. If you still have trouble in creating a contact form, please check our video instruction.

You can download code project PHP code to send email from a contact form here

In this tutorial, we use the default PHP mail function. This function works with almost all hostings, but if your hosting blocks it, you should contact them to enable it for your website or you can create PHP code to send email from contact form via SMTP.


Enjoy this blog? Please spread the word :)