PHP Sending HTML form data to an Email

In this article, we are going to learn how to collect the HTML form data and send an email using the PHP programming language.

In almost any web application, it is a common requirement to have a form. Gathering form data and delivering it to the inbox is a very convenient way and PHP makes this process more dynamic, as using this we can handle hundreds of responses per day. This process is based on the client server architecture. The HTML form is on the client side. When the user hits the submit button, the data will be sent to the URL of the page specified in the form action.

Sending form data to an Email

Here, we have created two pages- form.php and sendmail.php. The "form.php" page will collect the data entered by the user, and the file "sendmail.php" will collect them and send to an email.


The given form contains the following fields: to, from, subject, and message.

<title>Sending form data to an Email</title>
<link rel="stylesheet" href="" />
<div class="wrapper" style="width: 35%; margin: 0 auto;">
<form name="mailinfo" method="post" action="sendmail.php" class="form-signin">
<input type="text" name="to" size="40" class="form-control" placeholder="To:" />
<input type="text" name="from" size="40" class="form-control"  placeholder="From:" />
<input type="text" name="subject" size="40" class="form-control"  placeholder="Subject:" />
<textarea cols="50" rows="5" name="message" class="form-control" placeholder="Enter your message">
<input class="btn btn-small btn-primary" type="Submit" value="Send"/>

In the above file, we get the inputs from the user. When the user clicks on the send button, "the sendmail.php" page is loaded. On this page, we collect all the post data into variables, then we use the PHP mail function to send mail. The mail() function returns a value of 'True' if it is successful and 'False' if it fails.

<title>Sending form data to an Email</title>
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: '.$from.'\r\n';
$mailsent = mail($to, $subject, $message, $headers);
if($mailsent) {
echo 'Mail has been sent<br/><br/>';
echo '<b>To: </b>'.$to.'<br/>';
echo '<b>From: </b>'.$from.'<br/>';
echo '<b>Subject: </b>'.$subject.'<br/>';
echo '<b>Message: </b>'.$message.'<br/>';
} else {
echo 'Error in the message.';

Related Articles

PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
Remove duplicates from array PHP
PHP calculate percentage of total
Age calculator in PHP
Convert stdclass object to array PHP
PHP program to reverse a string without predefined function
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
PHP code to send email using SMTP
Submit a form data without page refresh using PHP, Ajax and Javascript
PHP Server Side Form Validation
PHP pagination example
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
How to create search filter in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL

Read more articles

General Knowledge

Learn Popular Language