添加其他字段时,电子邮件不会发送

时间:2017-10-01 19:32:54

标签: php html email

我在一个网站上有一份工作联系表格。没有任何性能问题。

我被要求添加一个必需的电话和地址字段,它已停止发送。我找不到任何编码错误,所以我在这里请求帮助。有时你需要一双新的眼睛来弄清楚出了什么问题。

注意:使用的jquery验证文件是未经编辑的标准Bootstrap版本。

提前谢谢!

HTML:

    <form name="sentMessage" id="contactForm" novalidate>
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <input type="text" id="name" class="form-control" placeholder="Name" required="required">
    <p class="help-block text-danger"></p>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <input type="email" id="email" class="form-control" placeholder="Email" required="required">
    <p class="help-block text-danger"></p>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <input type="text" id="phone" class="form-control" placeholder="Phone" required="required">
    <p class="help-block text-danger"></p>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group">
    <input type="text" id="address" class="form-control" placeholder="Address" required="required">
    <p class="help-block text-danger"></p>
    </div>
    </div>
    </div>
    <div class="form-group">
    <textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
    <p class="help-block text-danger"></p>
    </div>
    <div id="success"></div>
    <button type="submit" class="btn btn-custom btn-lg">Send Message</button>
    </form>

它背后的PHP:

    <?php
    // Check for empty fields
    if(empty($_POST['name'])        ||
       empty($_POST['email'])       ||
       empty($_POST['phone'])       ||
       empty($_POST['address'])         ||
       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'];
    $address = $_POST['address'];
    $message = $_POST['message'];

    // Create the email and send the message
    $to = 'Sample for Stack@Stack.com'; // 
    $email_subject = "XXX 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\nAddress: $address
    \n\nMessage:\n$message";
    $headers = "From: noreply@example.com\n"; // 
    $headers .= "Reply-To: $email_address"; 
    mail($to,$email_subject,$email_body,$headers);
    return true;            
    ?>

和JS:

    $(function() {

    $("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        event.preventDefault(); // prevent default submit behaviour
        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var address = $("input#address").val();
        var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            data: {
                name: name,
                email: email,
                phone: phone,
                address: address,
                message: message
            },
            cache: false,
            success: function() {
                // Success message
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');

                //clear all fields
                $('#contactForm').trigger("reset");
            },
            error: function() {
                // Fail message
                $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#contactForm').trigger("reset");
            },
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
    });
    });


    /*When clicking on Full hide fail/success boxes */
    $('#name').focus(function() {
    $('#success').html('');
    });

1 个答案:

答案 0 :(得分:1)

这是我完整的工作解决方案。

mail.php

<?php
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'test@test.com'; // 
$email_subject = "XXX 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\nAddress: $address
\n\nMessage:\n$message";
$headers = "From: noreply@example.com\n"; // 
$headers .= "Reply-To: $email_address"; 
if(mail($to,$email_subject,$email_body,$headers)) {
    return 'true';
} else {
    return 'false';
}
?>

mail.html(包括验证JS)

<form name="sentMessage" id="contactForm" novalidate>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" id="name" class="form-control" placeholder="Name" required="required">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <input type="email" id="email" class="form-control" placeholder="Email" required="required">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" id="phone" class="form-control" placeholder="Phone" required="required">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" id="address" class="form-control" placeholder="Address" required="required">
                <p class="help-block text-danger"></p>
            </div>
        </div>

        <div class="form-group">
            <textarea name="message" id="message" class="form-control" rows="4" placeholder="Message" required></textarea>
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div id="success"></div>
    <button type="submit" class="btn btn-custom btn-lg">Send Message</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <!-- or use local jquery -->
<script src="js/jqBootstrapValidation.js"></script>
<script>
$(function() {

    $("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        event.preventDefault(); // prevent default submit behaviour
        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var address = $("input#address").val();
        var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "mail.php",
            type: "POST",
            data: {
                name: name,
                email: email,
                phone: phone,
                address: address,
                message: message
            },
            cache: false,
            success: function() {
                // Success message
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');

                //clear all fields
                $('#contactForm').trigger("reset");
            },
            error: function() {
                // Fail message
                $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#contactForm').trigger("reset");
            },
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
    });
    });


    /*When clicking on Full hide fail/success boxes */
    $('#name').focus(function() {
    $('#success').html('');
    });
</script>

然后当然如果在/ js /

目录中有jqBootstrapValidation.js文件