php在Windows上发送邮件脚本编码问题

时间:2013-01-17 11:10:36

标签: php encoding sendmail

这个电子邮件脚本我的非英文字符有问题。如果我设置脚本发送电子邮件到我的Gmail帐户,我没有问题。但是,如果我将脚本设置为向我的域帐户发送电子邮件,并且如果我使用Windows Live邮件或使用microsoft outlook打开电子邮件,则电子邮件将无法读取。我必须去编码菜单,然后选择utf-8,然后我就可以阅读电子邮件了。

如果我在iMac邮件客户端上打开邮件,我没有问题。

我的客户认为这是一个大问题,并希望我解决它。有人可以帮忙吗?

以下是代码:

<?php
// send the form to the specify email

// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount@somemail.com";
$from = 'another@somemail.com';


//data
$msg = "Name: "  .$_POST['namesup']    ."<br>\n";
$msg .= "Email: "  .$_POST['emailsup']    ."<br>\n";
$msg .= "Phone: "  .$_POST['phonesup']    ."<br>\n";
$msg .= "Message: "  .$_POST['yourtextsup']    ."<br>\n";

//Headers
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;


//send
mail($to, $subject, $msg, $headers);
?>

任何帮助都将深表感谢。 卓然

3 个答案:

答案 0 :(得分:1)

查看此代码,它对我有用,

<?php

$subject = "mysite.com | contact form";
$to = "myaccount@somemail.com";
$from = 'another@somemail.com';

 $msg = '<html>
            <head>
            </head>
                <body>
                <p>
                               Name: ".$_POST['namesup']."<br>
                               Email: ".$_POST['emailsup']."<br>
                               Phone: ".$_POST['phonesup']."<br>
                               Message: ".$_POST['yourtextsup']."<br>
                    </p>
                </body>
        </html>';

    $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= "X-Mailer: PHP \r\n";
    $headers .= "From: <".$from. ">" ;

    mail($to,$subject,$msg,$headers);
?>

此行$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";已解决您的问题,如阿拉伯语或其他语言。

答案 1 :(得分:0)

我建议您确保自己的邮件是有效的HTML,包括<html><head>代码,并确保包含<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />代码。所以:

//data
$msg = '<html><head><meta http-equiv="Content-Type" content="text/html charset=UTF-8" /></head><body>';
$msg .= "Name: "  .$_POST['namesup']    ."<br>\n";
$msg .= "Email: "  .$_POST['emailsup']    ."<br>\n";
$msg .= "Phone: "  .$_POST['phonesup']    ."<br>\n";
$msg .= "Message: "  .$_POST['yourtextsup']    ."<br>\n";
$msg .= '</body></html>';

答案 2 :(得分:0)

尝试将脚本字符集更改为:

<?php
// send the form to the specify email

// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount@somemail.com";
$from = 'another@somemail.com';


//data
$msg = "Name: "  .$_POST['namesup']    ."<br>\n";
$msg .= "Email: "  .$_POST['emailsup']    ."<br>\n";
$msg .= "Phone: "  .$_POST['phonesup']    ."<br>\n";
$msg .= "Message: "  .$_POST['yourtextsup']    ."<br>\n";

//Headers
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: <".$from. ">" ;


//send
mail($to, $subject, $msg, $headers);
?>