简体   繁体   中英

how to set utf-8 character encoding in php file

I don't do php well. But I can read and understand what this code does.

Please bear with and help me out.

This is my HTML form

<html>
<?php header("Content-type: text/html; charset=utf-8"); ?>
</head>
<body style="background-color:#F9F0EB;">
    <form action="contact.php" method="post" accept-charset="utf-8">
        <p>Ваше имя:<input type="text" name="name" /></p>
        <p>E-mail:<input type="text" name="email" /></p>
        <p>Тема:<input type="text" name="subject" /></p>
        <p>Сообщение:<br />
        <textarea name="message" rows="5" cols="45"> </textarea></p>
        <p><input type="submit" value="Отправить"></p>
    </form>
</body>
</html>

And my php code is this:

<?php
/* Set e-mail recipient */
$myemail  = "tang.adil@mail.ru";


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your comments");


/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email
Comments:
$message

End of message
";

/* Send the message using mail() function */
if(mail($myemail, $subject, $message))
{
    $status = '<html style="background-color:#F9F0EB;">
                    <meta charset="utf-8"></meta>
                     <p>Спасибо за ваше сообщение!</p> 
              </html>';
}
else
{
    $status = "There was a problem sending your feedback, please try again     later.<br><br>";
}

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
<?php print $status; ?>

To make it clear. I saved all the documents in utf-8 format. I recieve the email correctly when i send it to the GMAIL.

However it doesn't work for Yandex or Mail.ru.

PHP sends but doesn't encode before sending. I tried a lot of scripts but none of them worked. Probably because I don't know php at all.

First: use php header code before any html - if you want. I still don't know why you're using header() function to set html encoding. It is sufficient if you enter meta tag:

<?php header("Content-type: text/html; charset=utf-8"); ?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color:#F9F0EB;">
    <form action="contact.php" method="post" accept-charset="utf-8">
        <p>Ваше имя:<input type="text" name="name" /></p>
        <p>E-mail:<input type="text" name="email" /></p>
        <p>Тема:<input type="text" name="subject" /></p>
        <p>Сообщение:<br />
        <textarea name="message" rows="5" cols="45"> </textarea></p>
        <p><input type="submit" value="Отправить"></p>
    </form>
</body>
</html>

But it all applies to the HTML page. For encoding email use this code below:

// set header
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";

// Send email
mail($to, $subject, $message, $headers);

More information here: http://php.net//manual/en/function.mail.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM