发送邮件表单不起作用

时间:2015-12-27 16:21:54

标签: php forms sendmail

$location ="/contact.php";   
if(isset($_POST[$form_names['email']], $_POST[$form_names['subject']], $_POST[$form_names['mess']])){

        if($csrf->check_valid('post')){

            $email = trim(htmlspecialchars($_POST[$form_names['email']]));
            $subject = trim(htmlspecialchars($_POST[$form_names['subject']]));
            $mess = trim(htmlspecialchars($_POST[$form_names['mess']]));

            if ($email == '' && $subject == '' && $mess == ''){
                // $message = "Success";

                $headers = "From: {$email}\r\n".
                "Content-Type: text/html; charset=ISO-8859-1\r\n";

                $current_ip = $_SERVER['REMOTE_ADDR'];

                $html_message = nl2br($mess);

                $sub = "CONTACT FORM: ".$subject;

                //send email
                $to = SITE_EMAIL;
                $the_mess = "IP: ".$current_ip." <br />
                        FROM: ".$email."<br />
                        MESSAGE: <p />"."$html_message";

                mail($to, $sub, $the_mess, $headers);

                $message = "<div class='alert alert-success'><button type='button' class='close' data-dismiss='alert'>x</button>Thank you, your message has been sent successfully.</div>";     

            } else {
                $message = "<div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert'>x</button>Please complete all required fields.</div>";
            }

        }

并形成:

<form action="<?php echo $location; ?>" method="post" class="form-horizontal" enctype="multipart/form-data">
<label for="subject">Choose Department:</label><br>
                    <select id="subject" name="<?php echo $form_names['subject']; ?>" class="form-control">
                        <option value="activation">Account activation</option>
                        <option value="suspension">Account suspension</option> 
                        <option value="auctions">Auctions</option>
                        <option value="complaint">Complaints</option>
                        <option value="others">Others</option>
                    </select>
            <br />
            <label for="email">Email Address</label>
                     <input type="text" class="form-control" name="<?php echo $form_names['email']; ?>" autocomplete="off" value="<?php echo htmlspecialchars($email); ?>" placeholder="Enter your Email Address">
            <br />
            <label for="email">Please describe your problem</label>
            <textarea type="text" class="form-control" style="height:111px;" name="<?php echo $form_names['mess']; ?>" placeholder="Your Message" required="required"><?php echo htmlspecialchars($mess); ?></textarea>
            <input class="btn btn-danger" type="submit" name="submit" value="Send Message" />
</form>

当我提交表单时,它会输出第二个$message

  

请填写所有必填字段。

我在这里找不到问题。我尝试了print_r(error_get_last());,但没有显示任何错误。任何帮助将非常感激。非常感谢你!

1 个答案:

答案 0 :(得分:4)

您的条件为if ($email == '' && $subject == '' && $mess == ''){,这意味着仅当所有字段为空时才会发送邮件。 我假设正确的条件应该像if ($email != '' && $subject != '' && $mess != ''){