Feedback form in PHP with sending почтуl. How to implement in a single file?

How to implement this code in one file?

<form action="./mail.php" method="post">

<!DOCTYPE html>
<html>
the <head>
<title>feedback Form in PHP sending mail</title>
</head>
the <body>
 <h2>feedback Form in PHP</h2>
 <!--The data entered by the user is processed by the code in mail.php-->
 <form action="./mail.php" method="post">
 <!--Tag is used to group related elements in a form-->
<fieldset>
 <!--The tag specifies the caption for the group of elements-->
 <legend>Leave a message</legend>
 Your name: 
 <!--Sets single-line text input field:-->
 <input type="text" name="name">
 <!--Is used for input fields that should contain email address.-->
E-mail:
 <input type="text" name="email">
 Phone number:
 <input type="text" name="phone">
Message:
 <!--The tag allows multiline text input.-->
 The text area can contain an unlimited number of characters-->
 <textarea rows="10" cols="45" name="message"></textarea>
 <!--Sets the button to send the form data to the form handler.-->
 <input type="submit" value="Send message">
</fieldset>
</form>
</body>
</html>


<?php
$to = "yourmail@gmail.com"; // email of recipient data from the form 
$tema = "contact Form PHP"; // the subject of the received email 
$message = "Your name: ".$_POST['name']."<br>";//assign the variable obtained from the form name=name
 $message .= "E-mail: ".$_POST['email']."<br>"; //received from the form name=email
$message .= "Telephone number: ".$_POST['phone']."<br>"; //received from the form name=phone
$message .= "Message: ".$_POST['message']."<br>"; //received from the form name=message
$headers = 'MIME-Version: 1.0' . "\r\n"; // header conforms to the format plus the newline character
 $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; // indicates the type of the sent content
mail($to, $tema, $message, $headers); //sends the recipient an email the values of the variables
?>
March 23rd 20 at 18:56
1 answer
March 23rd 20 at 18:58
We'll call the file index.php
Its contents will resemble the following:
<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$message = trim($_POST['message']);

// specify the sender's address, you can specify the address on the domain of Your site
$fromMail = 'admin@yousite.ru';
$fromName = 'yousite.ru Form';

// Here enter Your email
$emailTo = 'youemail@gmail.com';
$subject = 'contact Form php';
$subject = '=?utf-8?b?'. base64_encode($subject) .'?=';
$headers = "Content-type: text/plain; charset=\"utf-8\"\r\n";
$headers .= "From: ". $fromName ." <". $fromMail ."> \r\n";

// the body of the email
$body = "Received a letter from the site testsite.ru \n Name: $name\Telefon: $phone \n E-mail: $email\soobshenie: $message";

// the message will be sent if field not empty
if (strlen($phone) > 0) {
 $mail = mail($emailTo, $subject, $body, $headers, '-f'. $fromMail );
}

?>

<!DOCTYPE html>
<html>
the <head>
 <title>feedback Form in PHP sending mail</title>
</head>
the <body>
<h2>feedback Form in PHP</h2>
<!--The data entered by the user is processed by the code in mail.php-->
<form action="index.php" method="post">
 <!--Tag is used to group related elements in a form-->
<fieldset>
 <!--The tag specifies the caption for the group of elements-->
 <legend>Leave a message</legend>
 Your name:
 <!--Sets single-line text input field:-->
 <input type="text" name="name">
 <!--Is used for input fields that should contain email address.-->
E-mail:
 <input type="text" name="email">
 Phone number:
 <input type="text" name="phone">
Message:
 <!--The tag allows multiline text input.-->
 The text area can contain an unlimited number of characters-->
 <textarea rows="10" cols="45" name="message"></textarea>
 <!--Sets the button to send the form data to the form handler.-->
 <input type="submit" value="Send message">
</fieldset>
</form>
</body>
</html>

Replace youemail@gmail.com Your e-mail, he will receive the letters.
Replace yousite.ru to the address of the website where the form is placed

Find more questions by tags PHP