我已经搜索过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>';
}
}
}
?>
我的代码可以很好地发送电子邮件,但如果我在表单字段中放入中文字符,那么在电子邮件中它们就会显示为非常奇怪的符号。
请帮助!!
答案 0 :(得分:1)
您可以使用以下内容在电子邮件正文中使用中文:
$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
}