我的php邮件联系表有什么问题?

时间:2019-01-13 11:34:31

标签: php html5 phpmailer

我正在使用网站联系表格的模板。问题是,当我在服务器上对其进行测试时,无法发送消息。它只会给我我的错误消息,而不会发送。我将原始的发送文本翻译成德语时发生了什么变化?

我尝试调试var。我还尝试过echo一些字符​​串输入。

这是html代码:

                        <form id="contact-form" action="mail.php" method="post">
                        <div class="row">
                            <div class="col-md-6 form-group">
                                <label class="sr-only">Name</label>
                                <input type="text" class="form-control input-lg" name="name" placeholder="Name" >
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="col-md-6 form-group">
                                <label class="sr-only">Email</label>
                                <input type="email" class="form-control input-lg" name="email" placeholder="Email" >
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="col-md-12 form-group">
                                <label class="sr-only">Betreff</label>
                                <input type="text" class="form-control input-lg" name="subject" placeholder="Betreff" >
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="col-md-12 form-group">
                                <textarea class="form-control input-lg" rows="7" name="message" placeholder="Nachricht"></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="col-md-12 text-center">
                                <button type="submit" class="btn btn-lg btn-round btn-dark">Senden</button>
                            </div>
                        </div>
                    </form>

PHP:

<?php

//  POST GET.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Form Felder GET.
    $name = strip_tags(trim($_POST["name"]));
            $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $subject = trim($_POST["betreff"]);
    $message = trim($_POST["nachricht"]);

    // Check ob Daten an den mailer.
    if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // 400 (bad request) und raus.
        http_response_code(400);
        echo "Bitte füllen Sie alle Felder aus.";
        exit;
    }

    // Empfaenger.
    $recipient = "info@test.de";

    // Betreff.
    $subject = "Neue Anfrage von $name";

    // Inhalt.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Betreff: $subject\n\n";
    $email_content .= "Nachricht:\n$message\n";

    // Header.
    $email_headers = "Von: $name <$email>";

    // Senden.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // 200 (okay).
        http_response_code(200);
        echo "Vielen Dank! Deine Nachricht wurde versendet.";
    } else {
        // 500 (internal server error).
        http_response_code(500);
        echo "Oops! Hier ist ein Fehler passiert, deine Nachricht konnte nicht gesendet werden.";
    }

} else {
    // 403 (forbidden).
    http_response_code(403);
    echo "Es gibt Probleme mit deiner Anfrage, bitte versuche es ernuet.";
}

?>

1 个答案:

答案 0 :(得分:2)

您输入的主题名称是英语,但您的php文件是德语

为所有代码选择相同的lang可以避免混淆

将名称属性更改为德语以使其正常工作

<input type="text" class="form-control input-lg" name="betreff" placeholder="Betreff" >
<textarea class="form-control input-lg" rows="7" name="nachricht" placeholder="Nachricht">
</textarea>