0

我能够收到包含除了在表单中输入的电子邮件地址之外的所有信息的电子邮件,当我收到电子邮件时,我得到的是“无发件人”而不是此人的姓名。我是 PHP 新手,已经进行了几次 Google 搜索,但无法弄清楚我做错了什么。请帮忙。谢谢

这是我的 HTML:

<?php include("parts/doctype.php"); ?>

<?php include("parts/header.php"); ?>   

<div class="page-wrap">

    <?php include("parts/nav.php"); ?>  

        <div class="page-grid">

            <h1 class="site-section-title">Contact Us</h1>

            <div class="content-area">

                <form action="send-mail.php" method="POST">

                    <div class="contactName">
                        <h1 class="heading Name">Name</h1>
                        <label for="first-name" class="first_name">First Name</label>
                        <input type="text" id="first_name" name="first_name"  placeholder="Jane" autofocus required>
                        <label for="last_name" class="last_name">Last Name</label>
                        <input type="text" id="last_name" name="last_name" placeholder="Doe" required>
                    </div> <!-- end name -->

                    <div class="number">
                        <h1 class="heading">Phone Number</h1>
                        <label for="number" class="phone">Phone number</label>
                        <input type="tel" id="number" pattern="[0-9]{10}" name="sender_phone" placeholder="(###)-###-####">
                    </div> <!-- end number -->

                    <div class="email">
                        <h1 class="heading">Email Address</h1>
                        <label for="email" class="emailLabel">Email</label>
                        <input type="email" name="sender_email" placeholder="janedoe@gmail.com" required>
                    </div>

                    <div class="message">
                        <h1 class="heading">Message</h1>
                        <label for="message"></label>
                        <textarea name="sender_message" id="message" cols="30" rows="10" spellcheck="true" required> </textarea>
                    </div>

                    <button id="submit" class="send">Send</button>

                </form>

            </div> <!-- end content area -->

        </div> <!-- end grid -->

    </div> <!-- end page wrap -->

这是我的PHP:

<?php
$mail_to = 'removed address but I know it goes here'; // specify your email here

// Assigning data from the $_POST array to variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$message = $_POST['sender_message'];

// Construct email subject
$subject = 'Its time for a cupcake Message from visitor ' . $first_name;

// Construct email body
$body_message = 'From: ' . $first_name . $last_name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";  
$body_message .= 'Message: ' . $message;

// Construct email headers
$headers = 'From: ' . $first_name . "\r\n";

$headers .= 'Reply-To: ' . $mail_from . "\r\n";

$mail_sent = mail($mail_to, $subject, $body_message, $headers);


if ($mail_sent == true){ ?>
    <script language="javascript" type="text/javascript">
    alert('Thank you for the message. Should your message require a responce we will get back to you shortly.');
    window.location = 'contactus.php';
    </script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
    alert('Message not sent. Please, try your message again. Should this still not work please contact use through facebook or twitter both links located on our home page.');
    window.location = 'index.php';
</script>
<?php
}
?>
4

1 回答 1

0

您的标签应该与您的输入 ID“First-Name”匹配,而“First_Name”将不匹配。这是一个链接:http ://www.w3schools.com/tags/att_label_for.asp

除此之外它看起来还不错。如果它不能作为帖子工作,请尝试使用 get 作为您的表单方法。这更容易调试,因为名称值对将在 url 上传递,您至少可以验证您的 php 例程是否正确发送了值。如果这看起来不错,那么尝试在您的 php 中设置一个断点并逐步执行它。

希望这可以帮助!

于 2013-05-09T17:45:22.690 回答