Send.php不包括姓名和电子邮件

时间:2016-09-27 07:13:04

标签: php html email html-email contact

我目前拥有一个网站DowsingRod.net。我对PHP的了解并不是很好。我的send.php有些问题。它工作正常我收到一封包含消息的简单电子邮件。只有人们填写的姓名和电子邮件地址才能显示。

我的php如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
<meta name="viewport" content="width=device-width" />
</head>

<body>

<?php
$email_to = "darkboomba@gmail.com";
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$text = "Name: $name<br>
     Email: $email<br> 
     Message: $message";
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
$headers .= "From: <$email>" . "\r\n";
mail($email_to, "Message", $text, $headers);
?>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您应该尝试以下代码: -

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1" />
        <meta name="viewport" content="width=device-width" />
    </head>

    <body>

<?php
if(isset($_POST)&&!empty($_POST))
{
$email_to = "darkboomba@gmail.com";
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$text = "Name: $name<br>
     Email: $email<br> 
     Message: $message";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=utf-8" . "\r\n";
$headers .= "From: <$email>" . "\r\n";
mail($email_to, "Message", $text, $headers);    
}
?>
        <form method="POST">
            <label>Name</label>
            <input type="text" name="name" value=""/>
            <label>Email</label>
            <input type="email" name="email" value=""/>            
            <label>Message</label>
            <textArea name="message"></textarea>
            <input type="submit" value="Send Mail"/>
        </form>

    </body>
</html>

答案 1 :(得分:0)

  

这里,这只是尝试概述您的Send.php以及您的任意contact-us.php文件的外观。也许这有点帮助:希望。

ARBITRARY FILE:contact-us.php

<?php
    // CHECK IF SESSION EXIST BEFORE STARTING IT.
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }

    $errMessage = isset($_SESSION["errMessage"])    ? $_SESSION["errMessage"]   : "";
    $name       = isset($_SESSION["name"])          ? $_SESSION["name"]         : "";
    $email      = isset($_SESSION["email"])         ? $_SESSION["email"]        : "";
    $subject    = isset($_SESSION["subject"])       ? $_SESSION["subject"]      : "";
    $message    = isset($_SESSION["message"])       ? $_SESSION["message"]      : "";   
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1" />
        <meta name="viewport" content="width=device-width" />
    </head>

    <body>

    <!-- HTML FORM FROM WHICH WE GATHER OUR DATA FOR THE EMAIL -->
    <!-- HERE THE ENC-TYPE IS SET TO "multipart/form-data" IN CASE WE HAVE ATTACHMENTS OR SO... -->
    <!-- THE ACTION FOR THIS FORM IS SET TO "send.php" -->
    <div class="form-container">
        <div class="error-box">
            <?php echo $errMessage; ?>
        </div>

        <form name="email-form" id="email-form" class="email-form" enctype="multipart/form-data" method="post" action="send.php">
            <div class="form-group">
                <label for="name"   class="form-lbl">Name:</label>
                <input type="text"  class="form-control" value="<?php echo $name; ?>" id="name" name="name" placeholder="John Doe" />
            </div>

            <div class="form-group">
                <label for="email"  class="form-lbl">Email:</label>
                <input type="email" class="form-control" value="<?php echo $email; ?>" id="email" name="email" placeholder="john_doe@domain.tld"/>
            </div>

            <div class="form-group">
                <label for="subject"    class="form-lbl">Subject:</label>
                <input type="text"      class="form-control" value="<?php echo $subject; ?>" id="subject" name="subject" placeholder="Subject of this Email." />
            </div>

            <div class="form-group">
                <label for="message"   class="form-lbl">Message:</label>
                <textarea id="message" class="form-control" name="message" placeholder="Your message here..."><?php echo $message; ?></textarea>
            </div>

            <div class="form-group">
                <input type="submit"   class="form-control" name="submit" value="Send" id="submit" />
            </div>
        </form>
    </div>

</body>
</html>
<?php
    // UNSET ERROR MESSAGE FROM SESSION...
    unset($_SESSION["errMessage"]);

send.php文件:

<?php
    // CHECK IF SESSION EXIST BEFORE STARTING IT.
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }
    // CREATE A VARIABLE TO HOLD ERRORS (SHOULD YOU NEED IT).
    $errMessage     = $name = $email = $subject = $message = "";

    // NOW; CHECK THAT THE FORM HAS BEEN SUBMITTED
    if(isset($_POST['submit'])){
        // THEN SANITIZE ALL DATA COMING FROM THE FORM
        $name       = isset($_POST["name"])     ? htmlspecialchars(strip_tags(trim($_POST["name"])))    : null;
        $email      = isset($_POST["email"])    ? htmlspecialchars(strip_tags(trim($_POST["email"])))   : null;
        $subject    = isset($_POST["subject"])  ? htmlspecialchars(strip_tags(trim($_POST["subject"]))) : null;
        $message    = isset($_POST["message"])  ? htmlspecialchars(strip_tags(trim($_POST["message"]))) : null;
        $email_to   = "darkboomba@gmail.com";

        // BUILD THE ERROR MESSAGE:
        $errMessage.= !($name)    ? "The Name Field cannot be empty.<br>"     : "";
        $errMessage.= !($email)   ? "The Email Field cannot be empty.<br>"    : "";
        $errMessage.= !($subject) ? "The Email Subject is required.<br>"      : "";
        $errMessage.= !($message) ? "The Message Field cannot be empty.<br>"  : "";

        // SET SOME SESSION DATA:
        $_SESSION["errMessage"] = $errMessage;
        $_SESSION["name"]       = $name;
        $_SESSION["email"]      = $email;
        $_SESSION["subject"]    = $subject;
        $_SESSION["message"]    = $message;


        // SO! IF THE $errMessage IS EMPTY, THEN WE BUILD & SEND THE EMAIL
        // SINCE WE HAVE NO ERRORS... OTHERWISE WE JUST DISPLAY THE ERRORS AND BAIL...
        if($errMessage == "" || empty($errMessage)) {
            // SET THE HEADERS (MAKE IT HTML-COMPATIBLE)
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            $headers .= "From: <{$email}>" . "\r\n";

            // BUILD THE EMAIL BODY
            $text  = "Name: $name<br>";
            $text .= "Email: $email<br>";
            $text .= $subject? $subject : "Message: $message";

            // E-MAIL SUBJECT
            $subject = "Email from my Website.";

            // SEND OUT THE EMAIL:
            $status     = mail($email_to, $subject, $text, $headers);
            if($status){
                $name = $email = $subject = $message = "";
                $errMessage = "Email Successfully sent... Thanks for contacting us...";
                unset($_SESSION["name"]);
                unset($_SESSION["email"]);
                unset($_SESSION["subject"]);
                unset($_SESSION["message"]);
            }
        }
    }

    // FINALLY REDIRECT TO THE CONTACT-US PAGE...
    header("location: contact-us.php");