PHP 联系表的问题

Problems with PHP contact form

我创建的联系表有问题。当用户点击发送电子邮件按钮时,我收到带有 header 的电子邮件,但我看不到用户的输入。

所以基本上我能看到header($subject)和预写文本("This is an automated message"),但是我看不到$email和$message的内容.有什么问题吗?

<?php

$email = $_POST['email']; 
$message = $_POST['message'];

$to = "test@testemail.com"; 
$subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";

mail($to, $subject, $body); echo "Message Sent."; 
?>



<form id="contact-me-form" action="contact.php" name="contact_form "method="post">
    <input type="text" name="email" placeholder="Email Address">
    <textarea name="message" placeholder="Type Your Message Here"></textarea>
    <input id="sendEmail" type="submit" name="submit" value="Send">
</form>

您需要检查是否有post参数然后发送邮件

将您的代码更改为

     <?php
    if(isset($_POST['email']) && isset($_POST['message'])){
        $email = $_POST['email']; 
        $message = $_POST['message'];

        $to = "test@testemail.com"; 
        $subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";

        mail($to, $subject, $body); echo "Message Sent."; 
    }
    ?>



    <form id="contact-me-form" action="contact.php" name="contact_form "method="post">
        <input type="text" name="email" placeholder="Email Address">
        <textarea name="message" placeholder="Type Your Message Here"></textarea>
        <input id="sendEmail" type="submit" name="submit" value="Send">
    </form>

这样试试

<?php

$email = $_POST['email']; 
$message = $_POST['message'];

$to = "test@testemail.com"; 
$subject = "New Message!"; $body = "This is an automated message. Please do not reply to this email. \n\n $email \n\n $message";
$body= $_POST["message"]
if ($_SERVER["REQUEST_METHOD"] == "POST") {
mail($to, $subject, $body); echo "Message Sent."; 

}
?>



<form id="contact-me-form" action="contact.php" name="contact_form "method="post">
    <input type="text" name="email" placeholder="Email Address">
    <textarea name="message" placeholder="Type Your Message Here"></textarea>
    <input id="sendEmail" type="submit" name="submit" value="Send">
</form>

当用户在表单

中提交消息时,这将向test@testemail.com发送一封电子邮件