doujiacai4986 2016-10-30 13:53
浏览 82

外部PHP电子邮件请求响应500内部服务器错误

I am working on creating a contact us form for a website. I am using jquery to create an .click() function for an html button. then using ajax to call the email.php code. When I click the button the console displays the following error:

500 (Internal Server Error).

I'm new to ajax and php being used together so I have no idea what to do at this point.

Here is the HTML form:

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src = "js/callmail.js"></script>

</head>

<body ng-app = "">

            <div class = "container">

    <form name = "contact">

        <label for = "subject" class = "control-label">Subject:</label>
        <input name = "subject" id = "subject" ng-model = "subject" required/>
        <span class = "warning" ng-show="contact.subject.$touched && contact.subject.$invalid">You Must Enter a subject!</span>
        <span class = "success" ng-show="contact.subject.$touched && contact.subject.$valid">Valid!</span>

        <br/>

        <label for = "body" class = "control-label">Body:</label>
        <input name = "body" id = "body" ng-model = "body" required/>
        <span class = "warning" ng-show="contact.body.$touched && contact.body.$invalid">Your email must have a body!</span>
        <span class = "success" ng-show="contact.body.$touched && contact.body.$valid">Valid!</span>

        <br/>

        <label for = "signature" class = "control-label">Return Email:</label>
        <input name = "signature" id = "signature" ng-model = "signature" type = "email" required/>
        <span class = "warning" ng-show="contact.signature.$touched && contact.signature.$invalid">You must enter a valid return email!</span>
        <span class = "success" ng-show="contact.signature.$touched && contact.signature.$valid">Valid!</span>

        <br/>

        <button ng-disabled = "contact.$invalid" id = "sendmail" type = "submit" class = "btn btn-success">Submit</button>

    </form>

    </div>

and here is the jQuery/AJAX call to the php code:

$(function() {

$("#sendmail").click(function() {

    var data = {

        to: "myemail",
        subject: $("#signature").val() + ": " + $("#subject").val(),
        message: $("#body").val()   
    }

    $.ajax({

        type: "POST",
        url: "email.php",
        data: data,
        success: function() {

            alert("Message sent");

        }

    });

    console.log(data);

});

});

And finally, here is the php code that is responsible for sending out the email:

<?php   
    if ($_POST["submit"]) { 

        $subject = $_POST["subject"];
        $message = $_POST["message"];
        $headers = "From: " . $_POST["name"] . "Reply to: " . $_POST["signature"];

        mail("myemail", $subject, $message, $headers)) 

    }

?>

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • douyin4561 2016-10-30 14:43
    关注

    The problem with Error 500 is it's not specific. It just means "something went wrong". It's impossible to diagnose much of anything with just an Error 500.

    What you need to find out is what caused the Error 500, which would be in your PHP error log, if you know where that is.

    The first question I'd ask you is - what environment are you running PHP in? Is it on your computer or is it on a web server with an internet service provider? If you don't run a development environment on your PC I strongly advise you to get something simple like XAMPP installed and develop there. It's not ideal for advanced developers but for learning your way around PHP, it's good. Not least because it writes out proper error logs you can actually access and read :)

    The other thing is to look at the PHP documentation which provides some handy information about the mail function:

    http://php.net/manual/en/mail.requirements.php

    As you can see - mail() expects to be run on a system with access to a sendmail command. If the system you're running php on either doesn't have sendmail or might not have sendmail this could be your problem. However, without an error log to look at I'm making guesses.

    So my advice would be to try on a local installation of PHP first. And also with locally running PHP (eg using xampp) you'll have to set up SMTP server details - the server, username and password and security details you use for sending mail. But you will get access to much more comprehensive logs and error information.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题