Php邮件正文编码不正确

时间:2013-02-16 11:55:52

标签: php email

我有一个带有发送电子邮件表单的网站。不幸的是,今天我收到了客户的电子邮件,但邮件正文无法读取,因为它无法正常显示cyrilic符号。

这是php代码:

<?php
$owner_email = $_POST["owner_email"];
$headers = 'From:' . $_POST["email"];
$subject = 'Имате съобщение изпратено през формуляра на вашия WEB сайт от ' . $_POST["name"];
$messageBody = "";

if($_POST['name']!='nope'){
    $messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
    $messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}else{
    $headers = '';
}
if($_POST['state']!='nope'){        
    $messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['phone']!='nope'){        
    $messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}   
if($_POST['fax']!='nope'){      
    $messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
    $messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
    $messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}

if($_POST["stripHTML"] == 'true'){
    $messageBody = strip_tags($messageBody);
}

try{
    if(!mail($owner_email, $subject, $messageBody, $headers)){
        throw new Exception('mail failed');
    }else{
        echo 'mail sent';
    }
}catch(Exception $e){
    echo $e->getMessage() ."\n";
}
?>

我是php的新手,所以问题是:

我应该在此代码中更改什么才能使其与cyrilic格式一起使用?

4 个答案:

答案 0 :(得分:1)

$headers = ...行后添加:

$headers .= "\n".'MIME-Version: 1.0'."\n".'Content-Type: text/html; charset="UTF-8";'."\n".'Content-Transfer-Encoding: 7bit';

答案 1 :(得分:0)

$header .= "\nContent-type: text/plain; charset=\"utf-8\"";

并将此文件保存在utf-8

答案 2 :(得分:0)

尝试

htmlentities($ _ POST ['message'])而只是$ _POST ['message']

答案 3 :(得分:0)

\nContent-type: text/plain; charset=\"utf-8\"\nContent-Transfer-Encoding: 8bit添加到$header变量的末尾(您可以使用.=运算符执行此操作)。

普通的ascii中不存在西里尔字符,因此您需要使用不同的字符编码。