这是我的电子邮件表单代码。它工作得非常好 - 将所有内容发送到我的地址电子邮件。有一个问题和我的问题。我是如何做到的我可以回复从表格收到的电子邮件?我的意思是,用户在电子邮箱中键入他的电子邮件地址,所以我想简单回复它,所以发件人应该是作者的电子邮件。现在它是myname@mydomain-where-i-have-my-website.com。它应该采取这种形式'电子邮件'。我是一个很棒的php noobie。
HTML代码
<h2>Contact form</h2>
<div id="send_form_status"></div>
<form method="post" action="/kontakt.php" id="contact_form">
<div><label for="name">Subject</label></div>
<div><input type="text" name="name" id="name" required> </div>
<div><label for="email">E-mail</label></div>
<div><input type="email" name="email" required></div>
<div><label for="message">Message</label></div>
<div><textarea name="message" id="message" required></textarea></div>
<div><input type="submit" id="submint" name="submint" value="Send"></div>
</form>
PHP代码
<?php
header("content-type: application/json; charset=utf-8");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "myname@something.pl";
$subject = "Welcome";
if($name && $email && $message){
$headers = "MIME-Version: 1.0\r\nContent-type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: 8bit";
$message_body = "";
$message_body .= "Subject: " . $name . "\n";
$message_body .= "From: " . $email . "\n";
$message_body .= "Message: \n" . $message . "\n";
if(mail($to,$subject,$message_body,$headers)){
$json=array("status"=>1,"msg"=>"<p class='status_ok'>Message send.</p>");
}
else{
$json=array("status"=>0,"msg"=>"<p class='status_err'>Faild to send.</p>");
}
}
else{
$json=array("status"=>0,"msg"=>"<p class='status_err'>The all fields are required.</p>");
}
echo json_encode($json);
exit;
?>
已更新
正如我在评论中写的那样。要解决此问题,我删除了所有 $ message_body :
$message_body = "";
$message_body .= "Subject: " . $name . "\n";
$message_body .= "From: " . $email . "\n";
$message_body .= "Message: \n" . $message . "\n";
添加邮件标题和表单内容:
$mailheader = "MIME-Version: 1.0\r\n";
$mailheader = "Content-type: text/plain; charset=utf-8\r\n";
$mailheader = "From: ".$email." \n\r";
$formcontent="Subject: $name \n Message: \n $message \n" ;
并改变了这个:
if(mail($to,$subject,$message_body,$headers))
对此:
if(mail($to,$subject,$formcontent,$mailheader))