2

我在我的网站上创建了一个表单。我希望客户能够编写和发送希腊字符,但在邮件中我收到类似 Ï≥εÏ∞Ï≥ ελληÎ∏Î'κα 而不是希腊字符。我尝试使用以下代码将编码更改为 UTF-8:

mail($recipient, $subject, '=?UTF-8?B?'.base64_encode($content).'?=');

这个代码在我测试过的另一台服务器上工作,但在我的服务器上不起作用。有人可以帮忙吗?


这是我的完整 php 代码

<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
 } else {$name = 'No name entered';
 }
if ((isset($_POST['lastname'])) && (strlen(trim($_POST['lastname'])) > 0)) {
$lastname = stripslashes(strip_tags($_POST['lastname']));
 } else {$lastname = 'No name entered';
 }
if ((isset($_POST['nomos'])) && (strlen(trim($_POST['nomos'])) > 0)) {
$nomos = stripslashes(strip_tags($_POST['nomos']));
 } else {$nomos = 'No name entered';
 }
if ((isset($_POST['polh'])) && (strlen(trim($_POST['polh'])) > 0)) {
$polh = stripslashes(strip_tags($_POST['polh']));
 } else {$polh = 'No name entered';
 }
if ((isset($_POST['address'])) && (strlen(trim($_POST['address'])) > 0)) {
$address = stripslashes(strip_tags($_POST['address']));
 } else {$address = 'No name entered';
 }
if ((isset($_POST['TK'])) && (strlen(trim($_POST['TK'])) > 0)) {
$TK = stripslashes(strip_tags($_POST['TK']));
 } else {$TK = 'No name entered';
 }
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
 } else {$email = 'No email entered';
 }
if ((isset($_POST['phone'])) && (strlen(trim($_POST['phone'])) > 0)) {
$phone = stripslashes(strip_tags($_POST['phone']));
 } else {$phone = 'No phone entered';
 }
if ((isset($_POST['sxolia'])) && (strlen(trim($_POST['sxolia'])) > 0)) {
$sxolia = stripslashes(strip_tags($_POST['sxolia']));
 } else {$sxolia = 'Δεν υπάρχουν σχόλια';
 }

 ob_start();
 ?>
<html>
<head>

<style type="text/css">
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">

<tr bgcolor="#eeffee">
 <td>Όνομα</td>
 <td><?=$name; ?></td>
</tr> 
<tr bgcolor="#eeffee">
 <td>Επίθετο</td>
 <td><?=$lastname; ?></td>
</tr>  
<tr bgcolor="#eeffee">
 <td>Νομός</td>
 <td><?=$nomos; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>Πόλη</td>
 <td><?=$polh; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>Διεύθυνση</td>
 <td><?=$address; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>T.Κ</td>
 <td><?=$TK; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Τηλέφωνο</td>
 <td><?=$phone; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Email</td>
 <td><?=$email; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Σχόλια</td>
 <td><?=$sxolia; ?></td>
</tr>

</table>
</body>
</html>
<?
$body = ob_get_contents();

$to = 'mymail@yahoo.gr';
$email = 'mymail@yahoo.gr';
$fromaddress = "address";
$fromname = "Online Contact";

require ("phpmailer.php");

$mail = new PHPMailer();

$mail -> From = "address";
$mail -> FromName = "Book Order";

$mail -> AddAddress("mymail@gmail.com", "Name 5");

$mail -> WordWrap = 50;
$mail -> IsHTML(true);

$mail -> Subject = "Book Form:  Book form submitted";
$mail -> Body = $body;
$mail -> AltBody = "This is the text-only body";

if (!$mail -> Send()) {
$recipient = 'mymail@yahoo.gr';
$subject = 'Contact form failed';
$content = $body;

    $header = 'Content-type: text/html; charset=UTF-8' . "\r\n";
    mail($recepient,$subject, '=?UTF-8?B?'.base64_encode($content).'?=', $header);


exit ;
  }
 ?>
4

6 回答 6

3

主题或其他头字段值需要编码字语法,但正文不需要;在那里你可以使用 MIME 和Content-Type

$headerFields = array('MIME-Version: 1.0', 'Content-Type: text/plain;charset=utf-8');
mail($recipient, '=?UTF-8?B?'.base64_encode($subject).'?=', $content, implode("\r\n", $headerFields));
于 2012-06-23T18:07:48.857 回答
0

尝试这个。

$header = 'Content-type: text/plain; charset=UTF-8' . "\r\n";
mail($recepient,$subject, '=?UTF-8?B?'.base64_encode($content).'?=', $header);

如果您要发送 html 电子邮件,请使用 Content-type: text/html。

于 2012-06-23T17:54:30.583 回答
0

从哪里来$content?在 mail() 发送之前substr($content),您是否做了类似多字节安全的事情?

于 2012-06-23T17:57:22.003 回答
0

这发生在希腊字符 φίλε μου 上。我编辑了邮箱的设置。例如在 Roundcube 设置 > 显示消息 > 高级设置 > 默认字符集 = UTF-8 (Unicode)

于 2021-01-06T15:05:13.503 回答
0
/* Set internal character encoding to UTF-8 */
mb_internal_encoding("UTF-8");

$headers  = 'MIME-Version: 1.1';
$headers .= 'Content-type: text/html; charset=utf-8';
$headers .= "From: $name <$email>";
$headers .= "Return-Path: $emailTo";
$headers .= "Reply-To: $email";
$headers .= "X-Mailer: PHP/". phpversion();
于 2015-09-11T15:31:15.807 回答
-2

在处理同样的问题 8 小时后,我在网上找到了解决方案......

它对我有用(php、html 表单和 mysql)=>

http://akrabat.com/php/utf8-php-and-mysql/

于 2014-07-25T11:37:24.610 回答