用ajax发送电子邮件 - 错误

时间:2018-01-15 05:41:04

标签: javascript php jquery ajax email

我的电子邮件有问题。它告诉我网站成功,但它不发送我的邮件...我可能在某处犯了错误。我做了类似按钮的弹出框,其中所有来自FORM的输入。我找不到解决方案。 如果你想测试它:My web test page在这个网站上是“Szybki kontakt”这是发送电子邮件的按钮。但它在成功后不会发出信息。

这是我的PHP代码:

    <?php

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    header('Content-Type: text/html; charset=utf-8');
    die('Dostęp zabroniony');

}

if(isset($_POST['your-name']) && isset($_POST['your-email']) && isset($_POST['your-message'])) {

    $errors = array();

    if(empty($_POST['your-name'])) {
        array_push($errors, 'Podaj swoje imię');
    }

    if(!filter_var($_POST['your-email'], FILTER_VALIDATE_EMAIL)) {
        array_push($errors, 'Podaj poprawny adres e-mail');
    }

    if(empty($_POST['your-message'])) {
        array_push($errors, 'Napisz wiadomość');
    }

    if(count($errors) > 0) {

        echo json_encode($errors);

    } else {

        $to = 'my@gmail.com';
        $subject = 'Wiadomość z formularza';
        $message = $_POST['your-message'];
        $headers = 'From: ' . $_POST['your-email'] . "\r\n" .
            'Reply-To: ' . $_POST['your-email'] . "\r\n" .
            'Content-Type: text/plain;charset=utf-8\r\n' .
            'X-Mailer: PHP/' . phpversion();

        $mail_sent = mail($to, $subject, $message, $headers);

        if($mail_sent) {
            mail($to, $subject, $message, $headers);
            echo json_encode(array(
                'success' => 'Wiadomość została wysłana poprawnie'
            ));
        } else {
            echo json_encode(array(
                'error' => 'Wystąpił błąd podczas wysyłania wiadomości'
            ));
        }

    }

} else {

    echo json_encode(array(
        'error' => 'Przesłano niepoprawne pola formularza'
    ));

}

这是我的AJAX代码:

window.onload = scripts;
function scripts() {
    var form = document.querySelector('#form'),
        message = document.querySelector('#message');

    function showMessage(type, msg) {
        message.className = type;
        message.innerHTML = msg;
    }
    function sendEmail(e) {
        e.preventDefault();
        var fields = form.querySelectorAll('input, textarea'),
            data = {};

        [].forEach.call(fields, function(field) {
            data[field.name] = field.value;
        });

        AJAX({
            type: form.getAttribute("method"),
            url: form.getAttribute("action"),
            data: data,
            success: function(response, xhr) {
                var res = JSON.parse(response);
                if (Array.isArray(res)) {
                    showMessage("info", res.join("<br>"));
                } else if ("error" in res) {
                    showMessage("error", res.error);
                } else if ("success" in res) {
                    showMessage("success", res.success);
                    form.removeEventListener("submit", sendEmail, false);
                    form.querySelector(".submit-btn").setAttribute("disabled", "disabled");
                }
            }
        });
    }


    $(document).ready(function (){
        $('.msg-btn').on('click', function () {
            $(this).addClass('active');
            $('#form').addClass('popup');

        });
        $('.close').on('click', function () {
            setTimeout(function () {
                $('#form').removeClass('popup');
                $('.msg-btn').removeClass('active');
                $('#message').removeClass();
            }, 200);
        });
    });
    form.addEventListener("submit", sendEmail, false);
}

这是HTML:

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="assets/less/styles.less" type="text/css" rel="stylesheet/less"/>
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <script type="text/javascript" src="assets/js/jQuery.js"></script>
    <script type="text/javascript" src="assets/js/ajax.js"></script> 
    <script src="assets/js/less.js" type="text/javascript"></script>
    <script src="assets/js/app.js" type="text/javascript"></script>
    <title>Document</title>
</head>
<body>
    <div class="msg-btn">
        <h2>Szybki kontakt</h2>
        <div id="message"></div>

        <form id="form" action="mail.php" method="POST">
            <div class="close">X</div>
            <div class="input-field">
                <input type="text" name="your-name" placeholder="Twoje imie">
            </div>
            <div class="input-field">
                <input type="text" name="your-email" placeholder="Twoj email">
            </div>
            <div class="input-field">
                <textarea type="text" name="your-message" placeholder="Twoje wiadomosc"></textarea>
            </div>
            <div class="input-field">
                <button type="submit">Wyslij</button>
            </div>

        </form>
    </div>
    <form>
        <nav id="nav">
            <ul>
                <li><a data-page="bio.html">O mnie</a></li>
                <li><a data-page="prace.html">Prace</a></li>
                <li><a data-page="kontakt.html">Kontakt</a></li>
            </ul>
        </nav>
    </form>
    <div id="container">

    </div>
</body>
</html>

0 个答案:

没有答案