You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP邮件表单异常:含捷克语字符时出现乱码及发送问题

Fixing Czech Character Garble in Your PHP Email Form

Hey there! I totally get how frustrating it is when special Czech characters like č, ř, ž, ý, á, í turn into messy stuff like čřžýáí. This is 99% an encoding issue—luckily, we can fix it with a few simple tweaks to your PHP mail code, no advanced PHP skills required. Let's break it down step by step:

Step 1: Make sure your HTML page uses UTF-8 encoding

First, double-check your form's HTML file. In the <head> section, make sure you have this line (add it if it's missing):

<meta charset="UTF-8">

This ensures the browser sends the Czech characters to your PHP script in the right format.

Step 2: Update your PHP mail handling code

Find the PHP file that processes your form (it's probably named something like contact.php or sendmail.php). Open it up and make these changes:

1. Set PHP's internal encoding to UTF-8

At the very top of the PHP file (before any other code or HTML), add this line:

<?php
mb_internal_encoding('UTF-8');
header('Content-Type: text/html; charset=utf-8');
?>

This tells PHP to use UTF-8 for all text processing.

2. Fix the email headers to support UTF-8

Look for the part of the code that sets up email headers (it might look like $headers = "From: ..."). Replace or update that section to include UTF-8 encoding:

// Build your headers with UTF-8 support
$headers = "From: " . $_POST['your-name-field'] . " <" . $_POST['your-email-field'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['your-email-field'] . "\r\n";
// Add this line to force UTF-8 encoding for the email content
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; 
// If your email uses HTML instead of plain text, use this line instead:
// $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

3. Encode the email subject correctly

Czech characters in the subject line can also break, so wrap your subject variable with the mb_encode_mimeheader function. If your original code has something like:

$subject = "New message from your website: " . $_POST['subject'];

Change it to:

$subject = mb_encode_mimeheader("New message from your website: " . $_POST['subject'], 'UTF-8', 'Q');

This encodes the subject so email clients will display Czech characters properly.

4. Make sure you pass the headers to the mail() function

Finally, check that your mail() call includes the headers variable. It should look like this:

// Replace $to with your email address (where you want to receive messages)
$to = "your-email@example.com";
mail($to, $subject, $message, $headers);

If your original code didn't include the $headers at the end, add it—this is crucial for the encoding settings to take effect.

Step 3: Test it out

Save all your changes, upload the updated HTML and PHP files to your server, then test the form with Czech special characters. Send a message with čřžýáí and check your inbox—those characters should now display correctly instead of the garbled mess.

If you're still having trouble, double-check that you didn't miss any of the steps above, or share a snippet of your current PHP code (redact sensitive info like your email!) and I can help tweak it further.

内容的提问来源于stack exchange,提问作者Fyris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:55:26