I.Rayetzki I.Rayetzki - 3 months ago 11
HTML Question

Don't know what to write in my message variable?

I am a PHP noob, so i don't know what to write in my message variable. I need my data from the form to be sent to the email. I got a textarea, 2 inputs (name, email). Want the text from input to be sent to my email. Here code i have:

<?
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'rayetzkiillya@gmail.com';
$subject = 'Обратный звонок';
$message;
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Отправитель <from@example.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>

<form action="send.php" class="postcard" method="post">
<span>Your message:</span>
<textarea type="text" value="" required></textarea>
<div id="stripe1"></div>
<div id="stripe2"></div>
<img src="./images/Seal_of_the_United_States_Department_of_the_Post_Office.svg. png" alt="Oops" id="seal" />
<img src="./images/stamp.jpg" alt="Stamp" id="stamp" />
<div class="inputs">
<div class="inputs" id="input1"><label for="to" type="text" id="to">to: </label> <input type="text" value=" Me" readonly><div id="stripe3"></div></div>
<div class="inputs" id="input2"><label for="from" type="text" id="from">from: </label> <input type="text" id="input2"><div id="stripe3"></div></div>
<div class="inputs" id="input3"><label for="email" type="text" id="email">email: </label> <input type="text" id="input3"><div id="stripe3"></div></div>
<button type="button" class="btn btn-primary" id="send_button">Send</button>
</form>

Answer

You can make your message an HTML string, or just plain text. For plain text, you can do something like

$message .= "name: ".$_POST['name']."\r\n";
$message .= "Message: ".$_POST["theMessage"];

However you need to name your inputs in the HTML as well, so

<input type="text" id="input2" name="name">
<textarea type="text" value="" required name="theMessage"></textarea>

Or similar.

For the from address, you have to use change your header line change

$headers .= "From: Отправитель <from@example.com>\r\n";

to

$headers .= "From: ".$_POST["name"]." <".$_POST["from"].">\r\n";

And obviously name your input accordingly:

<input type="text" id="input3" name="from">

Also, as suggested by others, you should be sanitizing/validating these values before using them.