表格不发送邮件

时间:2016-02-08 15:09:51

标签: php forms html-email

我已经开始制作一个消息框,用户可以向我发送消息。当我运行代码时,方框工作正常,但我从未收到邮件,我也没有收到任何错误。谁能看出我做错了什么?

<?php 
        //set validation error flag as false
        $error = false;
        //check if form is submitted
        if (isset($_POST['submit']))
        {
            $name = trim($_POST['txt_name']);
            $fromemail = trim($_POST['txt_email']);
            $subject = trim($_POST['txt_subject']);
            $message = trim($_POST['txt_msg']);

            //name can contain only alpha characters and space
            if (!preg_match("/^[a-zA-Z ]+$/",$name))
            {
                $error = true;
                $name_error = "Please Enter Valid Name";
            }
            if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
            {
                $error = true;
                $fromemail_error = "Please Enter Valid Email ID";
            }
            if(empty($subject))
            {
                $error = true;
                $subject_error = "Please Enter Your Subject";
            }
            if(empty($message))
            {
                $error = true;
                $message_error = "Please Enter Your Message";
            }
            if (!$error)
            {
                //send mail
                $toemail = "test@stackoverflow.com";
                $subject = "Enquiry from Visitor " . $name;
                $body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Message: \n $message";
                $headers = "From: $fromemail\n";
                $headers .= "Reply-To: $fromemail";

                if (mail ($toemail, $subject, $body, $headers))
                    $alertmsg  = '<div class="alert alert-success text-center">Message sent successfully.  We will get back to you shortly!</div>';
                else
                    $alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail.  Please try again later.</div>';
            }
        }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>Bootstrap 3 Contact Form Example</title>
        <meta content="width=device-width, initial-scale=1.0" name="viewport" >
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3 well">
                <form role="form" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contactform">
                <fieldset>
                    <legend>Bootstrap Contact Form</legend>

                    <div class="form-group">
                        <div class="col-md-12">
                            <label for="txt_name" class="control-label">Name</label>
                        </div>
                        <div class="col-md-12">
                            <input class="form-control" name="txt_name" placeholder="Your Full Name" type="text" value="<?php if($error) echo $name; ?>" />
                            <span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12">
                            <label for="txt_email" class="control-label">Email ID</label>
                        </div>
                        <div class="col-md-12">
                            <input class="form-control" name="txt_email" placeholder="Your Email ID" type="text" value="<?php if($error) echo $fromemail; ?>" />
                            <span class="text-danger"><?php if (isset($fromemail_error)) echo $fromemail_error; ?></span> 
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12">
                            <label for="txt_subject" class="control-label">Subject</label>
                        </div>
                        <div class="col-md-12">
                            <input class="form-control" name="txt_subject" placeholder="Your Subject" type="text" value="<?php if($error) echo $subject; ?>" />
                            <span class="text-danger"><?php if (isset($subject_error)) echo $subject_error; ?></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12">
                            <label for="txt_msg" class="control-label">Message</label>
                        </div>
                        <div class="col-md-12">
                            <textarea class="form-control" name="txt_msg" rows="4" placeholder="Your Message"><?php if($error) echo $message; ?></textarea>
                            <span class="text-danger"><?php if (isset($message_error)) echo $message_error; ?></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-12">
                            <input name="submit" type="submit" class="btn btn-primary" value="Send" />
                        </div>
                    </div>
                </fieldset>
                </form>
                <?php if (isset($alertmsg)) { echo $alertmsg; } ?>
            </div>
        </div>
    </div>
    </body>
    </html>

0 个答案:

没有答案