How to Send Html Form Data to Email using PHP

15 May 2022 | PHP


In this tutorial, you will learn how to Send Html Form Data to Email using PHP, most people want to get data from html form to send via email after submitting the form. I will show you how to get data from an html form and send it to an email address.

You can send Plain Text Email or HTML Formatted Email using PHP built-in mail() function, I will show you both example.

This is the basic syntax of PHP mail() function.

mail(to, subject, message, headers, parameters)

Follow the steps below to send HTML form data directly to an email address.

Step 1. Create Contact Form

First, we need to create an HTML contact form to send details.

contact.php

<?php include "sendmail.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Contact Form</title>
	<style>
	body {
		font-family:"Open Sans", Helvetica, Arial, sans-serif;
		line-height: 1.5;
	}
	.container {
		max-width:500px;
		width:100%;
		margin:0 auto;
	}
	form{
		width: 100%;
	}
	label{
		font-weight: bold;
		margin-bottom: 10px;
	}
	input, textarea {
		font-family:"Open Sans", Helvetica, Arial, sans-serif;
		width:100%;
		border:1px solid #CCC;
		background:#FFF;
		margin:0 0 5px;
		padding:10px;
	}
	fieldset {
		border: medium none !important;
		margin: 0 0 10px;
		min-width: 100%;
		padding: 0;
		width: 100%;
	}
	button{
		cursor:pointer;
		width: 100%;
		border:none;
		background:rgb(3, 153, 212);
		color:#FFF;
		margin:0 0 5px;
		padding:10px;
		font-size:15px;
	}
</style>
</head>
<body>

	<div class="container">
		<h1>Contact Form</h1>
		<form method="post">
			<fieldset>
				<label>Name</label>
				<input type="text" name="name" placeholder="Enter Name">
			</fieldset>
			<fieldset>
				<label>Mobile</label>
				<input type="text" name="mobile" placeholder="Enter Mobile">
			</fieldset>
			<fieldset>
				<label>Email</label>
				<input type="email" name="email" placeholder="Enter Email">
			</fieldset>
			<fieldset>
				<label>Message</label>
				<textarea name="message" placeholder="Type your Message..."></textarea>
			</fieldset>
			<fieldset>
				<button type="submit" name="submit">SUBMIT</button>
			</fieldset>
		</form>
	</div>

</body>
</html>

Step 2. Create PHP File to send Email

In this step, we need to create a php file to submit the contact form and send the form data to an email address.

Send Plain Text Email Example

sendmail.php

<?php
if(isset($_POST['submit'])){
	$to = "[email protected]"; // Your email address
	$name = $_POST['name'];
	$from = $_POST['email'];
	$phone = $_POST['mobile'];
	$message = $_POST['message'];
	$subject = "Contact Form Details";
	$headers = "From:" . $from;
	$result = mail($to,$subject,$message,$headers);

	if ($result) {
		echo '<script type="text/javascript">alert("Your Message was sent Successfully!");</script>';
		echo '<script type="text/javascript">window.location.href = window.location.href;</script>';

	}else{
		echo '<script type="text/javascript">alert("Sorry! Message was not sent, Try again Later.");</script>';
		echo '<script type="text/javascript">window.location.href = window.location.href;</script>';
	}
}
?>

HTML Formatted Email Example

sendmail.php

<?php
$message = ""; 
if(isset($_POST['submit'])){
	$to = "[email protected]"; // Your email address
	$name = $_POST['name'];
	$from = $_POST['email'];
	$phone = $_POST['mobile'];
	$message = "<!DOCTYPE html>
	<html>
	<head>
	</head>
	<body>
	<table rules='all' border='1' style='border-color: #666;' cellpadding='10'>
    <tr style='background: #eee;'><td colspan='2'><strong>Contact Form Details</strong> </td></tr>
    <tr>
        <td><strong>Name:</strong></td>
        <td>".$_POST['name']."</td>
    </tr>
    <tr>
        <td><strong>Mobile:</strong></td>
        <td>".$_POST['mobile']."</td>
    </tr>
    <tr>
        <td><strong>Email:</strong></td>
        <td>".$_POST['email']."</td>
    </tr>
    <tr>
        <td><strong>Message:</strong></td>
        <td>".$_POST['message']."</td>
    </tr>
	</table>
	</body>
	</html>";
	
	$subject = "Contact Form Details";
	
	// 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";
	
	$headers .= "From:" . $from . "\r\n";
	
	$result = mail($to,$subject,$message,$headers);
	if ($result) {
		// $message = "Your Message was sent Successfully!";
		echo '<script type="text/javascript">alert("Your Message was sent Successfully!");</script>';
		echo '<script type="text/javascript">window.location.href = window.location.href;</script>';

	}else{
		// $message = "Sorry! Message was not sent, Try again Later.";
		echo '<script type="text/javascript">alert("Sorry! Message was not sent, Try again Later.");</script>';
		echo '<script type="text/javascript">window.location.href = window.location.href;</script>';
	}
	// header('Location: contact.php');
}
?>

Now the above code is ready to run.

Buddy! I hope you relished the tutorial, and it was good to see you again. Keep learning. Keep visiting.