简单的PHP联系表单在提交时不发送

时间:2017-02-07 03:12:30

标签: php html forms email

我在nodeJS站点中有一个简单的php邮件服务器连接来处理联系人消息。出于某种原因(我从本地测试它),它不会将消息路由到指定的电子邮件地址。我提到从本地运行它,因为我听说它可能无法工作,除非它来自我在Heroku上运行的实时站点。但是,我从另一个应用程序中剪切并粘贴了代码,该代码可以在本地运行。这是contact_me.php的代码:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
    empty($_POST['email'])      ||
    empty($_POST['phone'])      ||
    empty($_POST['message'])    ||
    !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
    {
    echo "No arguments Provided!";
        return false;
    }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'myapp@gmail.com'; // Add your email address inbetween the ''      replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact  form.\n\n"."Here are the details:\n\nName: $name\n\nEmail:        $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To:$email_address";
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

我在某个地方有点困惑,但似乎我在html(id='success')中创建的提交按钮应该以某种方式连接到$to对象,以便在单击按钮时,邮件将发送到与$to关联的电子邮件地址。这是我的HTML。我在这里错过了什么吗?

<div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h2 class="featurette-heading" style="text-align: center;">Message</h2>
                <h3 class="text-muted">Want to book an appointment? Send me a message and I'll contact you. Make sure you include your name, phone number, email, and when you'd like to book. It's that easy!</h3>
                <form name="sentMessage" id="contactForm" novalidate>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-primary">Send</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
</div>    

我从第一个回答开始修改了这条消息。第一个答案似乎有效,但我仍然收到一个错误,阻止请求通过。这是错误消息:     enter image description here

1 个答案:

答案 0 :(得分:1)

您使用的是PHP,而不是node.js!

你错过了非常基本的东西:你还没有给出你的表单字段名称,所以当你检查它们时,那​​里什么也没有!您正在检查nameemailphonemessage,但您的字段根本没有名称属性。您需要在HTML中命名它们 - 不,id值不起作用:

<div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h2 class="featurette-heading" style="text-align: center;">Message</h2>
                <h3 class="text-muted">Want to book an appointment? Send me a message and I'll contact you. Make sure you include your name, phone number, email, and when you'd like to book. It's that easy!</h3>
                <form name="sentMessage" id="contactForm" novalidate>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="text" class="form-control" placeholder="Name" id="name" name="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="email" class="form-control" placeholder="Email Address" id="email" name="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <input type="tel" class="form-control" placeholder="Phone Number" id="phone" name="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div style="background-color: white; border: 1px solid #0085a1;" class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label></label>
                            <textarea rows="5" class="form-control" placeholder="Message" id="message" name="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-primary">Send</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
</div>