我已经搜索了StackOverflow试图找到答案,但为类似问题提供的答案都没有解决我的问题。
所以我这里有一个非常基本的表单,我希望它能够接受汉字,然后通过电子邮件发送数据。(注意:中文字符出现在我的网页上没有问题)
.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
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>';
}
}
}
?>
我的代码运行良好,可以发送电子邮件,但是如果我在表单字段中输入中文字符,则在电子邮件中它们会成为非常奇怪的符号。
请帮忙!!
您可以使用以下内容在电子邮件正文中使用中文:
$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
}