PHP中的编码

PHP中的编码,php,Php,我已经做了一个私人网站的联系形式,发送一个html邮件给我。但是收到邮件有一个问题。我得到的不是“ä”、“ö”或“ü”?“-符号 这是我的PHP代码: <?php /* variables */ $error = ''; $headers = ''; $empfaenger = ''; $mail = ''; $subject = ''; $message = ''; $an

我已经做了一个私人网站的联系形式,发送一个html邮件给我。但是收到邮件有一个问题。我得到的不是“ä”、“ö”或“ü”?“-符号

这是我的PHP代码:

<?php
    /* variables */
    $error       = '';
    $headers     = '';
    $empfaenger  = '';
    $mail        = '';
    $subject     = '';
    $message     = '';
    $anrede      = '';
    $name        = '';
    $vorname     = '';
    $firma       = '';
    $strasse     = '';
    $nr          = '';
    $plz         = '';
    $ort         = '';
    $land        = '';
    $telefon     = '';
    $fax         = '';
    $REMOTE_ADDR = '';


    if (isset($_POST['submit'])) {
        /* Anrede */
        $anrede = $_POST['anrede'];

        /* Nachname */
        $name = $_POST['name'];

        /* Vorname*/
        $vorname = $_POST['vorname'];

        /* Firma */
        $firma = $_POST['firma'];

        /* Strasse */
        $strasse = $_POST['strasse'];

        /* Nr */
        $nr = $_POST['nr'];

        /* PLZ */
        $plz = $_POST['plz'];

        /* Ort */
        $ort = $_POST['ort'];

        /* Land */
        $land = $_POST['land'];

        /* Telefonnummer */
        $telefon = $_POST['telefon'];

        /* Fax */
        $fax = $_POST['fax'];

        /* Empfänger */
        $empfaenger = 'office@awidal.com';

        /* Absender */
        $mail = $_POST['mail'];

        /* Text */
        $text = $_POST['text'];

        /* Betreff */
        $subject = 'Kontaktformular Webseite';

        /* Nachricht zusammensetzen (HTML-Email) */
        $message = '<html>
                                <head>
                                    <title>Kontaktformular</title>
                                    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                                    </head>
                                    <body>
                                    <table>
                                        <tr>
                                            <td colspan="2"><b>Kontaktformular</b></td>
                                        </tr>
                                        <tr>
                                            <td>Name</td>
                                            <td>' . $anrede . '&nbsp;' . $name . '&nbsp;' . $vorname . '</td>
                                        </tr>
                                        <tr>
                                            <td>Firma</td>
                                            <td>' . $firma . '</td>
                                        </tr>
                                        <tr>
                                            <td>Strasse/Nr</td>
                                            <td>' . $strasse . '&nbsp;' . $nr . '</td>
                                        </tr>
                                        <tr>
                                            <td>PLZ/Ort</td>
                                            <td>' . $plz . '&nbsp;' . $ort . '</td>
                                        </tr>
                                        <tr>
                                            <td>Land</td>
                                            <td>' . $land . '</td>
                                        </tr>
                                        <tr>
                                            <td>Telefon</td>
                                            <td>' . $telefon . '</td>
                                        </tr>
                                        <tr>
                                            <td>Fax</td>
                                            <td>' . $fax . '</td>
                                        </tr>
                                        <tr>
                                            <td>E-mail</td>
                                            <td>' . $mail . '</td>
                                        </tr>
                                        <tr>
                                            <td>Nachricht</td>
                                            <td>' . $text . '</td>
                                        </tr>
                                </table>
                                </body>
                                </html>';


        /* Header der Mail zusammenbauen */
        $headers .= 'From:' . $mail . "\n";
        /* $headers .= 'Reply-To:' . $reply . "\n"; */
        $headers .= 'X-Mailer: PHP/' . phpversion() . "\n";
        $headers .= 'X-Sender-IP: ' . $REMOTE_ADDR . "\n";
        $headers .= "Content-type: text/html\n";

        function convertUmlaute($mess)
        {
            /* Umlaute konvertieren */
            $pattern1  = "/�/";
            $replace1  = "&auml;"; // &#228
            $mess      = preg_replace($pattern1, $replace1, $mess);
            $pattern2  = "/�/";
            $replace2  = "&ouml;"; // &#246
            $mess      = preg_replace($pattern2, $replace2, $mess);
            $pattern3  = "/�/";
            $replace3  = "&uuml;"; // &#252
            $mess      = preg_replace($pattern3, $replace3, $mess);
            $pattern1a = "/�/";
            $replace1a = "&Auml;"; // &#196
            $mess      = preg_replace($pattern1a, $replace1a, $mess);
            $pattern2a = "/�/";
            $replace2a = "&Ouml;"; // &#214
            $mess      = preg_replace($pattern2a, $replace2a, $mess);
            $pattern3a = "/�/";
            $replace3a = "&Uuml;"; // &#220
            $mess      = preg_replace($pattern3a, $replace3a, $mess);
            $pattern4  = "/�/";
            $replace4  = "&#Eacute;";
            $mess      = preg_replace($pattern4, $replace4, $mess);
            $pattern4a = "/�/";
            $replace4a = "&eacute;";
            $mess      = preg_replace($pattern4a, $replace4a, $mess);
            return $mess;
        }

        convertUmlaute($message);

        /* Variablen, um Eingaben des Formulars zu überprüfen */
        $ok_firma   = 0;
        $ok_anrede  = 0;
        $ok_name    = 0;
        $ok_vorname = 0;
        $ok_strasse = 0;
        $ok_nr      = 0;
        $ok_plz     = 0;
        $ok_ort     = 0;
        $ok_mail    = 0;
        $ok_text    = 0;

        /* Variable, welche den gültigen Bereich der Postleitzahl (PLZ) setzt */
        $int_options = array(
            "options" => array(
                "min_range" => 1000,
                "max_range" => 99999
            )
        );

        /* Schaut, dass im Minimum die Pflichtfelder ausgefüllt sind */
        if (empty($name) OR empty($vorname) OR empty($mail) OR empty($text) OR empty($anrede)) {
            echo '<p style="color:red">&nbsp;Sie haben nicht alle Pflichfelder ausgef&uuml;llt!</p>';
        } else {
            $ok_anrede = 1;
            $ok_text   = 1;

            /* Überprüft den Wert des Feldes NACHNAME */
            if (filter_var($name, FILTER_VALIDATE_INT)) {
                echo '<p style="color:red">&nbsp;Das Feld <b>Nachname</b> darf nur aus Buchstaben bestehen!</p>';
            } else {
                $ok_name = 1;
            }

            /* Überprüft den Wert des Feldes VORNAME */
            if (filter_var($vorname, FILTER_VALIDATE_INT)) {
                echo '<p style="color:red">&nbsp;Das Feld <b>Vorname</b> darf nur aus Buchstaben bestehen!</p>';
            } else {
                $ok_vorname = 1;
            }

            /* Überprüft, ob eine korrekte EMAIL eingegeben wurde */
            if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
                echo '<p style="color:red">&nbsp;Bitte geben Sie eine korrekte Email-Adresse ein!</p>';
            }

            else {
                $ok_mail = 1;
            }

            /* Überprüft den Wert des Feldes FIRMA */
            if (!empty($firma)) {
                if (filter_var($firma, FILTER_VALIDATE_INT)) {
                    echo '<p style="color:red">&nbsp;Das Feld <b>Firma</b> darf nicht nur aus Zahlen bestehen!</p>';
                } else {
                    $ok_firma = 1;
                }
            } else {
                $ok_firma = 1;
            }

            /* Überprüft den Wert des Feldes STRASSE */
            if (!empty($strasse)) {
                if (filter_var($strasse, FILTER_VALIDATE_INT)) {
                    echo '<p style="color:red">&nbsp;Das Feld <b>Strasse</b> darf nur aus Buchstaben bestehen!</p>';
                } else {
                    $ok_strasse = 1;
                }
            } else {
                $ok_strasse = 1;
            }

            /* Überprüft den Wert des Feldes NR */
            if (!empty($nr)) {
                if (!filter_var($nr, FILTER_VALIDATE_INT)) {
                    echo '<p style="color:red">&nbsp;Das Feld <b>Nr</b> darf nur aus Zahlen bestehen!</p>';
                } else {
                    $ok_nr = 1;
                }
            } else {
                $ok_nr = 1;
            }

            /* Überprüft den Wert des Feldes PLZ */
            if (!empty($plz)) {
                if (!filter_var($plz, FILTER_VALIDATE_INT, $int_options)) {
                    echo '<p style="color:red">&nbsp;Das Feld <b>PLZ</b> darf nur aus Zahlen im Bereich von 1000 bis 99999 bestehen!</p>';
                } else {
                    $ok_plz = 1;
                }
            } else {
                $ok_plz = 1;
            }

            /* Überprüft den Wert des Feldes ORT */
            if (!empty($ort)) {
                if (filter_var($ort, FILTER_VALIDATE_INT)) {
                    echo '<p style="color:red">&nbsp;Das Feld <b>Ort</b> darf nur aus Buchstaben bestehen!</p>';
                } else {
                    $ok_ort = 1;
                }
            } else {
                $ok_ort = 1;
            }

            /* Mail wird gesendet, falls alle Eingaben korrekt sind und den definierten Werten bzw. Datentypen entsprechen */
            if (($ok_anrede + $ok_text + $ok_name + $ok_vorname + $ok_mail + $ok_firma + $ok_strasse + $ok_nr + $ok_plz + $ok_ort) == 10) {
                mail($empfaenger, $subject, $message, $headers);
                echo '<p style="color:red">&nbsp;Mail erfolgreich gesendet!</p>';
            } else {
                echo '<p style="color:red">&nbsp;Mail konnte nicht gesendet werden!</p>';
            }

        }
    }
    ?>

在php函数
convertUmlaute($mess)
中,您可以看到?-符号。在我的php代码中,这些是正方形


你能帮我吗?

构造一个电子邮件头,在每个邮件客户机中都能正确呈现,这并不是一件容易的事

考虑使用像phpmailer这样的库。

这似乎是一个字符集编码问题。您不需要“凌乱的”
convertUmlaute()
函数

只需更改电子邮件标题中的一些行即可:

改变

$headers .= "Content-type: text/html\n";
进入:

或用于测试

$headers .= "Content-Type: text/plain; charset=utf-8" . "\n"; 

顺便说一句:德语“Umlaut”“䔓ö”“ü”的正确英语术语是变异元音

,到底是什么问题?提示:通过写“转换变异”,你将解决一些与本网站普通程序员完全不同的问题。这么说之后,也许是时候开始了解这一点了:在邮件中,我只收到“?”符号,而不是“ä”、“ö”或“ü”。@Asator:是的。还有一条消息告诉你:通过在这里创建代码示例,你已经在源代码中创建了?(!)。这就是为什么现在开始学习字符集非常重要的原因。否则就很难和你谈论这个问题。@Astor你的意思是“我有编码问题”。建议将所有内容都设置为UTF8,这样您就不需要重新编码/修改/修改任何内容。我添加了以下内容:它不起作用。它现在可以工作了。我添加了htmlentities函数,而不是自己创建的函数。非常感谢你!
$headers .= "Content-Type: text/plain; charset=utf-8" . "\n";