PHP - 表单显示符号而不是字母

时间:2016-05-03 15:29:00

标签: php html forms encoding character-encoding

问候我需要PHP表单文本编码方面的帮助。当我通过表单发送电子邮件时 - 我收到它们时没有像“ąčęėįšų”这样的字符。这是我到目前为止所尝试的:

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Kontaktai</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="js/main.js"></script>
</head>
<body class="one">
<div id="holder">
    <div id="full-width-background">
        <div class="container">
        <?php include 'header.php';?>

        <?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $human = intval($_POST['human']);
        $from = 'ITConnect'; 
        $from .= 'Content-Type: text/html; charset="UTF-8"';
        $from .= 'Content-Transfer-Encoding: 7bit';  
        $to = 'mail@mail.lt'; 
        $subject = 'Kliento kontaktas';
        $body = "Nuo: $name\n\n Adresatas: $email\n\n Tekstas:\n $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Įveskite savo vardą';
        }
        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Įveskite teisingą el. pašto adresą';
        }
        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Parašykite žinutę';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Anti-Spam atsakymas neteisingas';
        }
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Dėkojame, žinutė išsiūsta! Atsakysime per vieną darbo dieną.</div>';
    } else {
        $result='<div class="alert alert-danger">Žinutė nebuvo išsiūsta. Atsiprašome už nesklandumus.</div>';
    }
}
    }

如果在消息中我发送的内容类似“ąąčaac” - 我收到的是:??? aac。请帮忙!

1 个答案:

答案 0 :(得分:1)

我认为问题是您没有明确设置邮件消息的内容类型......

而不是使用

if (mail($to, $subject, $body, $from)) {
   ...
}

试试这个:

$headers = "From: $from <$from>\r\n". 
           "MIME-Version: 1.0" . "\r\n" . 
           "Content-type: text/html; charset=UTF-8" . "\r\n"; 
if (mail($to, $subject, $body, $headers)) {
   ...
}