将Unicode UTF8添加到电子邮件中

时间:2013-11-15 08:55:51

标签: php email unicode utf-8

任何人都可以帮助我,请问如何为这些字符添加utf-8 Unicode?

  

žýáťčšľľzŽŘ

现在电子邮件看起来像这样

  

Dostalistenovéºsprávu。 Tusúpodrobnosti:Jméno:tá   Tel.ÄÃslo:4996611

<?php

$EmailFrom = "...";
$EmailTo = "mail@mail";
$Subject = "Zpráva s ";
$name = Trim(stripslashes($_POST['name'])); 
$phone = Trim(stripslashes($_POST['phone'])); 
$email = Trim(stripslashes($_POST['email'])); 
$message = Trim(stripslashes($_POST['message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

由于

1 个答案:

答案 0 :(得分:3)

你的脚本中有两个不明确的部分,其中没有考虑字符集。您的表单可能不是发送UTF-8,也可能是您在电子邮件中发送UTF-8,但客户端期望编码不同。

  1. 我假设您正在使用表单发布数据。确保将其设置为从浏览器发送UTF-8。字符串现在将是UTF-8编码。

    <form action="myform" accept-charset="UTF-8">
    
  2. 您需要在电子邮件标题中添加字符集和传输编码标题:

    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: 8bit");
    
  3. BTW,重定向用户的更快方法是在响应中设置Location标头。使用:

    header('Location: contactthanks.php')