发送电子邮件和PHP的字符编码

时间:2013-11-16 08:36:53

标签: php validation character-encoding character

我有一个简单的联系页面,可以使用以下代码发送包含联系人数据的电子邮件。但我无法阅读其中任何一个,因为这些字符不可读。根据我的网站内容,我猜这些电子邮件是阿拉伯语。我在浏览器视图中尝试了很多字符编码,但问题仍然存在。例如,这是一封电子邮件的一部分:اØÙ...دپÙر 我想知道该代码有什么问题,如何修复它以及如何阅读之前电子邮件中的文本?

<?php

$EmailFrom = "info@example.com";
$EmailTo = "info@example.com";
$Subject = Trim(stripslashes($_POST['City'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$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 .= $Tel;
$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\">";
}
?>

2 个答案:

答案 0 :(得分:1)

试试这个

$header = "From: <$EmailFrom> \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-Type: text/html;charset=utf-8";

$success = mail($EmailTo, $Subject, $Body, $header );

答案 1 :(得分:0)

不要使用自己的解决方案撰写和发送电子邮件,你可能会弄错。使用一些电子邮件类,例如PHPMailer。只设置了CharSet属性:

$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';

或[swiftmailer] [2]或Pear's Mail Mime