简体   繁体   中英

PHP contact form email being received with no sender and email is not being populated in the body

I am able to receive email with all the information except for the email address that is entered in the form, and when I receive the email, I get "No Sender" instead of the person's name. I am new to PHP and have done several Google searches but can't figure out what I am doing wrong. Please help. Thanks

This is my 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 -->

And this is my 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
}
?>

Your Label for should match your input id "First-Name" and "First_Name" won't match. Here is a link: http://www.w3schools.com/tags/att_label_for.asp

Other than that it looks pretty good. If its not working as a post try using a get for your form method. That is easier to debug because the name value pairs will be passed on the url and you can at least verify that your php routine is sent the values correctly. If that looks good then try setting a break point in your php and stepping through it.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM