You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

免费Bootstrap模板HTML联系表单无法发邮件并报错求助

联系表单无法正常工作问题排查求助

我正在学习基础网站开发,目前遇到联系表单无法正常工作的问题。使用的是ThemeWagon的免费Bootstrap 4 HTML5建筑公司网站模板,已上传至购买的虚拟主机。

该联系表单依赖4个文件:HTML表单文件、用于设置收件邮箱的contact.php文件、contact.js文件以及负责数据验证的jqBootstrapValidation.min.js文件。以下是各文件代码:

HTML表单代码

<div class="col-md-6">
    <div class="contact-form">
        <div id="success"></div>
        <form name="sentMessage" id="contactForm" novalidate="novalidate">
            <div class="control-group">
                <input type="text" class="form-control" id="name" placeholder="Your Name" required="required" data-validation-required-message="Please enter your name" />
                <p class="help-block text"></p>
            </div>
            <div class="control-group">
                <input type="email" class="form-control" id="email" placeholder="Your Email" required="required" data-validation-required-message="Please enter your email" />
                <p class="help-block text"></p>
            </div>
            <div class="control-group">
                <input type="text" class="form-control" id="subject" placeholder="How can we help?" required="required" data-validation-required-message="Please enter a subject" />
                <p class="help-block text-"></p>
            </div>
            <div class="control-group">
                <textarea class="form-control" id="message" placeholder="Message" required="required" data-validation-required-message="Please enter your message"></textarea>
                <p class="help-block text"></p>
            </div>
            <div>
                <button class="btn" type="submit" id="sendMessageButton">Send Message</button>
            </div>
        </form>
    </div>
</div>

contact.php代码

<?php
    if(empty($_POST['name']) || empty($_POST['subject']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
      http_response_code(500);
      exit();
    }

    $name = strip_tags(htmlspecialchars($_POST['name']));
    $email = strip_tags(htmlspecialchars($_POST['email']));
    $m_subject = strip_tags(htmlspecialchars($_POST['subject']));
    $message = strip_tags(htmlspecialchars($_POST['message']));

    $to = "myemail@mail.com"; // Change this email to yours //
    $subject = "$m_subject:  $name";
    $body = "You have received a new message from your website contact form.

"."Here are the details:

Name: $name


Email: $email

Subject: $m_subject

Message: $message";
    $header = "From: $email";
    $header .= "Reply-To: $email";  

    if(!mail($to, $subject, $body, $header))
      http_response_code(500);
?>

contact.js代码

$(function () {

    $("#contactForm input, #contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function ($form, event, errors) {
        },
        submitSuccess: function ($form, event) {
            event.preventDefault();
            var name = $("input#name").val();
            var email = $("input#email").val();
            var subject = $("input#subject").val();
            var message = $("textarea#message").val();

            $this = $("#sendMessageButton");
            $this.prop("disabled", true);

            $.ajax({
                url: "contact.php",
                type: "POST",
                data: {
                    name: name,
                    email: email,
                    subject: subject,
                    message: message
                },
                cache: false,
                success: function () {
                    $('#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>');
                    $('#contactForm').trigger("reset");
                },
                error: function () {
                    $('#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>").text("Sorry " + name + ", it seems that our mail server is not responding. Please try again later!"));
                    $('#success > .alert-danger').append('</div>');
                    $('#contactForm').trigger("reset");
                },
                complete: function () {
                    setTimeout(function () {
                        $this.prop("disabled", false);
                    }, 1000);
                }
            });
        },
        filter: function () {
            return $(this).is(":visible");
        },
    });

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

$('#name').focus(function () {
    $('#success').html('');
});

我已将contact.php文件中的收件邮箱修改为个人邮箱以及主机上创建的自定义邮箱,但线上提交表单时,始终收到错误提示:Sorry [姓名], it seems that our mail server is not responding. Please try again later!。试过检查PHP文件首尾标签,将$to改为$email_to,问题仍未解决,求排查线索。


排查方向建议

  • 检查虚拟主机邮件服务状态:多数虚拟主机默认禁用PHP mail() 函数,或要求用绑定域名的邮箱作为发件人。登录主机控制面板,确认邮件功能是否开启,是否需配置SMTP参数。
  • 修复PHP邮件头格式:当前$header拼接缺少换行符,HTTP头必须用\r\n分隔,修正后代码:
    $header = "From: $email\r\n";
    $header .= "Reply-To: $email\r\n";
    
    格式错误会导致邮件被服务器拒收。
  • 添加错误日志定位问题:在contact.php的mail()判断后增加日志记录,便于查看具体失败原因:
    if(!mail($to, $subject, $body, $header)){
        error_log("邮件发送失败:" . error_get_last()['message']);
        http_response_code(500);
    }
    
    然后查看主机的PHP错误日志,获取详细报错信息。
  • 验证文件路径与权限:确认contact.php的路径和contact.js中url: "contact.php"完全一致,文件权限设置为644,避免因权限不足无法执行。
  • 测试基础邮件发送功能:编写最简测试脚本,单独运行确认主机可正常发信:
    <?php
    $to = "你的收件邮箱";
    $subject = "测试邮件";
    $body = "测试内容";
    $headers = "From: 你的域名邮箱@xxx.com\r\n";
    if(mail($to, $subject, $body, $headers)){
        echo "发送成功";
    } else {
        echo "发送失败";
        print_r(error_get_last());
    }
    ?>
    

内容的提问来源于stack exchange,提问作者Toshgarcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:20:00