2016-08-12 63 views
-3

我是一個PHP noob,所以我不知道在我的消息變量中寫什麼。我需要將表單中的數據發送到電子郵件。我得到了一個textarea,2個輸入(姓名,電子郵件)。希望將來自輸入的文本發送到我的電子郵件。下面的代碼我有:不知道在我的消息變量中寫什麼?

<? 
if((isset($_POST['name'])&&$_POST['name']!="") && (isset($_POST['phone'])&&$_POST['phone']!="")){ 
    $to = '[email protected]'; 
    $subject = 'Обратный звонок'; 
    $message; 
    $headers = "Content-type: text/html; charset=utf-8 \r\n"; 
    $headers .= "From: Отправитель <[email protected]>\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> 
+0

[Here](http://php.net/manual/en/tutorial.forms.php)。 – Script47

+0

btw,你可以用if(!empty($ _ POST ['name'])替換if((isset($ _ POST ['name'])&& $ _ POST ['name']!=「」))! –

+0

使用'empty'時,在您的驗證中添加'trim'。 – Script47

回答

0

你可以讓你的消息的HTML字符串,或只是簡單的文本。 對於純文本,你可以這樣做

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

但是你需要在HTML命名您的輸入,以及, 所以

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

或同級。

對於從地址,你必須使用更改標題行 變化

$headers .= "From: Отправитель <[email protected]>\r\n"; 

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

很顯然據此命名您的輸入:

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

而且,正如其他人所建議的那樣,您應該先清除/驗證這些值 使用它們。

+0

我希望這個消息來自一個textarea value –

+0

我已經添加了更改。你需要命名textarea,然後像在PHP中的其他變量一樣對待它。 – SilicaGel

+0

沒有驗證或消毒,真的嗎?你必須真正相信你的用戶。 – Script47

相關問題