How to send a web page by email? The simplest form of sending data by email using HTML and PHP How to send a bookmark by email
One of the most popular functions on the site is the application or order form, the data from which is sent by email to the site owner. As a rule, such forms are simple and consist of two or three fields for data entry. How to create such an order form? This requires the use of language HTML markup and the PHP programming language.
The HTML markup language itself is simple; you just need to figure out how and where to put certain tags. With the PHP programming language, things are a little more complicated.
For a programmer, creating such a form is not difficult, but for an HTML layout designer, some actions may seem difficult.
Create a data submission form in html
The first line will be as follows
Now let's put everything together.
Now let's make the fields in the form mandatory. We have the following code:
Create a file that accepts data from the HTML form
This will be a file called send.php
In the file, at the first stage, you need to accept data from the post array. To do this, we create two variables:
$fio = $_POST["fio"];
$email = $_POST["email"];
Variable names in PHP are preceded by a $ sign, and a semicolon is placed at the end of each line. $_POST is an array into which data from the form is sent. In the html form, the sending method is specified as method="post". Thus, two variables are taken from html forms. To protect your site, you need to pass these variables through several filters - php functions.
The first function will convert all the characters that the user will try to add to the form:
In this case, new variables are not created in php, but existing ones are used. What the filter will do is transform the character "<" в "<". Также он поступить с другими символами, встречающимися в html коде.
The second function decodes the URL if the user tries to add it to the form.
$fio = urldecode($fio);
$email = urldecode($email);
With the third function we will remove spaces from the beginning and end of the line, if any:
$fio = trim($fio);
$email = trim($email);
There are other functions that allow you to filter php variables. Their use depends on how concerned you are that an attacker will try to add program code to this html email submission form.
Validation of data transferred from HTML form to PHP file
In order to check whether this code works and whether data is being transferred, you can simply display it on the screen using the echo function:
echo $fio;
echo "
";
echo $email;
The second line here is needed to separate the output of php variables into different lines.
Sending received data from an HTML form to email using PHP
To send data by email, you need to use the mail function in PHP.
mail("to which address to send", "subject of the letter", "Message (body of the letter)","From: from which email the letter is sent \r\n");
For example, you need to send data to the email of the site owner or manager [email protected].
The subject of the letter should be clear, and the message of the letter should contain what the user specified in the HTML form.
mail(" [email protected]", "Application from the site", "Full name:".$fio.". E-mail: ".$email ,"From: [email protected]\r\n");
It is necessary to add a condition that will check whether the form was sent using PHP to the specified email address.
if (mail(" [email protected]", "Order from the site", "Full name:".$fio.". E-mail: ".$email ,"From: [email protected]\r\n"))
{
echo "message sent successfully";
) else (
}
Thus, the program code of the send.php file, which will send the HTML form data to email, will look like this:
$fio = $_POST["fio"];
$email = $_POST["email"];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$fio = urldecode($fio);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
//echo $fio;
//echo "
";
//echo $email;
if (mail(" [email protected]", "Application from the site", "Full name:".$fio.". E-mail: ".$email ,"From: [email protected]\r\n"))
( echo "message sent successfully";
) else (
echo "errors occurred while sending the message";
}?>
Three lines to check whether the data is being transferred to the file are commented out. If necessary, they can be removed, since they were needed only for debugging.
We place the HTML and PHP code for submitting the form in one file
In the comments to this article, many people ask the question of how to make sure that both the HTML form and the PHP code for sending data to email are in one file, and not two.
To implement this work, you need to place the HTML code of the form in the send.php file and add a condition that will check for the presence of variables in the POST array (this array is sent from the form). That is, if the variables in the array do not exist, then you need to show the user the form. Otherwise, you need to receive data from the array and send it to the recipient.
Let's see how to change the PHP code in the send.php file:
//check if variables exist in the POST array
if(!isset($_POST["fio"]) and !isset($_POST["email"]))(
?> ) else (
//show the form
$fio = $_POST["fio"];
$email = $_POST["email"];
$fio = htmlspecialchars($fio);
$email = htmlspecialchars($email);
$fio = urldecode($fio);
$email = urldecode($email);
$fio = trim($fio);
$email = trim($email);
if (mail(" [email protected]", "Application from the site", "Full name:".$fio.". E-mail: ".$email ,"From: [email protected]\r\n"))(
echo "Message sent successfully";
) else (
echo "Errors occurred while sending the message";
}
}
?>
We check the existence of a variable in the POST array with the isset() PHP function. An exclamation mark before this function in a condition means negation. That is, if the variable does not exist, then we need to show our form. If I hadn’t put the exclamation point, the condition would literally mean “if exists, then show the form.” And this is wrong in our case. Naturally, you can rename it to index.php. If you rename the file, do not forget to rename the file name in the line