收到的PHP联系表单电子邮件没有发件人和电子邮件,因此未在正文中填充

时间:2013-05-09 17:26:53

标签: php

我能够收到包含表单中输入的电子邮件地址以外的所有信息的电子邮件,当我收到电子邮件时,我会收到“No Sender”而不是该人的姓名。我是PHP的新手,已经完成了几次谷歌搜索,但无法弄清楚我做错了什么。请帮忙。感谢

这是我的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
}
?>

1 个答案:

答案 0 :(得分:0)

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

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

希望这有帮助!