简体   繁体   English

如何让PHP通过电子邮件发送汉字?

[英]How can I have PHP send Chinese characters in an email?

I've already scoured StackOverflow trying to find the answer but none of the answers provided to similar questions have solved my problem. 我已经搜寻过StackOverflow,试图找到答案,但是提供给类似问题的答案都没有解决我的问题。

So I have a pretty basic form here, and I want it to be able to accept Chinese characters, and then send the data in an email. 因此,我在这里有一个非常基本的形式,我希望它能够接受中文字符,然后通过电子邮件发送数据。 (Note: Chinese characters appear on my webpage with no issue) (注意:中文字符不会出现在我的网页上)

HTML: HTML:

<!doctype html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<link href="tour.css" rel="stylesheet">
</head>
<body>

<?php include('tour-form.php') ?>

<div class="form">
      <div class="row">
        <div class="container">
                 <br>

            <form class="form-horizontal" role="form" method="post" action="tour.html">

                <div class="form-group">
                        <label for="name" class="col-xs-2 control-label">姓名</label>
                        <div class="col-xs-10">
                            <input type="text" class="form-control" id="name" name="name" placeholder="您叫?" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                            <?php echo "<p class='text-danger'>$errName</p>";?>
                        </div>
                    </div>

           </form> 
        </div>
    </div>
</div>

</body>
</html>

php: 的PHP:

    <?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];

        $to = 'email@website.com'; 
        $subject = 'New Form';

        $body = "Customer: $name\n";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }
    // If there are no errors, send the email
if (!$errName) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Form Submitted!</div>';
    } else {
        $result='<div class="alert alert-danger">Error!</div>';
    }
}
    }
?>

My code works well enough to send the email, but if I put Chinese characters in the form field, in the email they come out as really weird symbols. 我的代码可以很好地发送电子邮件,但如果我在表单字段中输入中文字符,则在电子邮件中,它们会变成真正的怪异符号。

Please help!! 请帮忙!!

You can use Chinese language inside the email body by using this: 您可以通过以下方式在电子邮件正文中使用中文:

$headers = 'From: youremail@example.com' . "\r\n"; 
$headers .= "Content-Type: text/html; charset=UTF-8"; // add header
if (mail ($to, $subject, $body, $headers)) {
    // your stuff
} else {
    // your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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