Reputation: 13
I have made a private website with a contact form which sends an html mail to me. But there is a problem with getting mails. Instead of "ä", "ö" or "ü" I get "?"-signs.
This is my PHP code:
<?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 = '[email protected]';
/* 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 . ' ' . $name . ' ' . $vorname . '</td>
</tr>
<tr>
<td>Firma</td>
<td>' . $firma . '</td>
</tr>
<tr>
<td>Strasse/Nr</td>
<td>' . $strasse . ' ' . $nr . '</td>
</tr>
<tr>
<td>PLZ/Ort</td>
<td>' . $plz . ' ' . $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 = "ä"; // ä
$mess = preg_replace($pattern1, $replace1, $mess);
$pattern2 = "/�/";
$replace2 = "ö"; // ö
$mess = preg_replace($pattern2, $replace2, $mess);
$pattern3 = "/�/";
$replace3 = "ü"; // ü
$mess = preg_replace($pattern3, $replace3, $mess);
$pattern1a = "/�/";
$replace1a = "Ä"; // Ä
$mess = preg_replace($pattern1a, $replace1a, $mess);
$pattern2a = "/�/";
$replace2a = "Ö"; // Ö
$mess = preg_replace($pattern2a, $replace2a, $mess);
$pattern3a = "/�/";
$replace3a = "Ü"; // Ü
$mess = preg_replace($pattern3a, $replace3a, $mess);
$pattern4 = "/�/";
$replace4 = "&#Eacute;";
$mess = preg_replace($pattern4, $replace4, $mess);
$pattern4a = "/�/";
$replace4a = "é";
$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"> Sie haben nicht alle Pflichfelder ausgefü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"> 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"> 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"> 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"> 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"> 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"> 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"> 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"> 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"> Mail erfolgreich gesendet!</p>';
} else {
echo '<p style="color:red"> Mail konnte nicht gesendet werden!</p>';
}
}
}
?>
In the php-function convertUmlaute($mess)
you can see ?-signs. In my php code these are squares.
Can you help me?
Upvotes: 0
Views: 224
Reputation: 5133
Constructing a E-Mail header, which renders correctly in every Mail-Client is not that easy.
Consider using a library like phpmailer.
It seems to be a charset-encoding problem. You don't need your "messy" convertUmlaute()
function...
Just change some lines in your E-Mail header:
Change
$headers .= "Content-type: text/html\n";
into:
$headers .= "Content-Type: text/html; charset=utf-8" . "\n";
// better style of "\n"
Or for testing
$headers .= "Content-Type: text/plain; charset=utf-8" . "\n";
BTW: The correct english term for that german 'Umlaut' 'ä', 'ö', 'ü' is mutated vowel
Upvotes: 2