邮件工作时,Ajax POST请求会产生500内部服务器错误

时间:2015-03-31 12:56:50

标签: php ajax mailer

我的问题是我通过ajax请求得到500内部服务器错误,但mailer.php仍然发送邮件。我无法弄清楚为什么会出现服务器错误,我看了几个似乎相似的问题,但这对我没有帮助,所以我希望有人知道该怎么做。

我的form.js看起来像这样:

$(function() {

// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$(form).submit(function(e) {
    // Stop the browser from submitting the form.
    e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    })
        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#lname').val('');
            $('#email').val('');
            $('#size_s').val('0');
            $('#size_m').val('0');
            $('#size_l').val('0');
            $('#size_xl').val('0');
            $('#size_xxl').val('0');
            $('#strasse').val('');
            $('#plzort').val('');
        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
           if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
           } else {
                $(formMessages).text('Oops! An error occured and your message could not be sent.');
            }
        });

});});

我的Php:

 <?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $lname = strip_tags(trim($_POST["lname"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $strasse = strip_tags(trim($_POST["strasse"]));
    $ort = $_POST["ortplz"];
    $size_s = $_POST['size_s'];
    $size_m = $_POST['size_m'];
    $size_l = $_POST['size_l'];
    $size_xl = $_POST['size_xl'];
    $size_xxl = $_POST['size_xxl'];
    $preis = 67.18;
    $gesamtpreis = number_format(($size_xxl + $size_xl + $size_l + $size_m) * $preis, 2);
    $preisOMwst = number_format($gesamtpreis * 0.81, 2);
    $Mwst = number_format($gesamtpreis * 0.19, 2);
// Check that data was sent to the mailer.
if (empty($lname) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Set a 400 (bad request) response code and exit.
    http_response_code(400);
    echo "Etwas ist schief gelaufen! Versuchen Sie es erneut.Fehlercode: 400 (bad request)";
    exit;
}

// Empfängeradresse angeben
//FIXME: RECIPIENT
$recipient = "john.doe@samplemail.com";

// Set the email subject.
$subject = "Neue Bestellung von $lname";

// E-Mail Inhaltaufbau
$email_content = "Name: $lname\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Straße: $strasse\n\n";
$email_content .= "Ort: $ort\n\n";
if ($size_s == 0 && $size_m == 0 && $size_l == 0 && $size_xl == 0 && $size_xxl == 0) {
    //ERROR
} else {
    $email_content .= "Größe: S: $size_s | M: $size_m   | L: $size_l | XL: $size_xl  | XXL: $size_xxl\n";
}
$email_content .= "\n Netto: $preisOMwst € \n Zzgl. 19% MwSt: $Mwst € \n Zu Zahlen: $gesamtpreis €";
// Email headers
$headers = "From: $lname <$email>" . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";

// Senden

   if (mail($recipient, $subject, $email_content, $headers)) {
       // Set a 200 (okay) response code.
       http_response_code(200);
       echo "Danke! Ihre Bestellung wurde aufgegeben.";

   } else {
       // Set a 500 (internal server error) response code.
       http_response_code(500);
       echo "Etwas ist schief gelaufen! Versuchen Sie es erneut. Fehlercode: 500 (Internal Server Error)";
   }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Etwas ist schief gelaufen! Versuchen Sie es erneut. Fehlercode: 403 (forbidden)";
    }

我的HTML格式:

     <form id="ajax-contact" action="mailer.php" method="get">
<input id="fname" type="text" name="fname" required>
        <p><label for="lname" class="input-label first"> Vor- und Nachname: </label><input id="lname" type="text" name="lname" required></p>
        <p><label for="email" class="input-label first">  E-Mail: </label><input id="email" type="email" name="email" required></p>
        <p><label for="strasse" class="input-label first">  Straße und Hausnummer: </label><input id="strasse" type="text" name="strasse" required></p>
        <p><label for="ortplz" class="input-label first">  PLZ und Ort: </label><input id="ortplz" type="text" name="ortplz" required></p>
        <p style="margin-bottom:10px;"><span class="input-label first" style="float:none;">  Menge: </span></p>
        <div style="overflow: hidden; display:flex;">
        <p><label for="size_s" class="input-label first size">  S: </label><input id="size_s" type="text" name="size_s" value="0" readonly="true" style="color:gray !important;"></p>
        <p><label for="size_m" class="input-label size">  M: </label><input id="size_m" type="text" name="size_m" value="0"></p>
        <p><label for="size_l" class="input-label size">  L: </label><input id="size_l" type="text" name="size_l" value="0"></p>
        <p><label for="size_xl" class="input-label size">  XL: </label><input id="size_xl" type="text" name="size_xl" value="0"></p>
        <p><label for="size_xxl" class="input-label size">  XXL: </label><input id="size_xxl" type="text" name="size_xxl" value="0"></p>
        </div>
        <button id="open" class="my_popup_open">Bestellung überprüfen</button>
        <p id="form-messages"></p>

        </form>

    <div id="my_popup">
        <input type="checkbox" name="agb" value="agb" id="agb" form="ajax-contact"> Ich akzeptiere die <a class="nonblock" href="agb.html">AGB</a> von mencreation und stimme dem <a class="nonblock" href="agb.html#widerruf">Widerrufsrecht</a> zu!</p><br>
            <form><input id="senden" type="submit" value="Zahlungspflichtig bestellen" form="ajax-contact"></form>

            <!-- Add an optional button to close the popup -->
            <button id="close" class="my_popup_close">Abbrechen</button>


        </div>

我发现如果我把if(!isset($_POST["fname"]))(这是我的蜜罐字段)放在发送部分周围,我会得到OK代码200.所以如果它在PHP中看起来像这样:

// Senden
if(!isset($_POST["fname"])) {
    if (mail($recipient, $subject, $email_content, $headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Danke! Ihre Bestellung wurde aufgegeben.";

    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Etwas ist schief gelaufen! Versuchen Sie es erneut. 

Fehlercode: 500 (Internal Server Error)";
        }
    }
    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "Etwas ist schief gelaufen! Versuchen Sie es erneut. Fehlercode: 403 (forbidden)";
    }

但邮件不再发送了。

我只是不知道怎么可能,因为在200代码的情况下,一个jax请求是成功的,所以邮件应该发送邮件不应该吗?

顺便说一句:我很久以前就找到了这个邮件,并在另一个网站上使用它,工作得很好......

提前致谢!

0 个答案:

没有答案