2012-01-27 61 views
0

我想用我的语言编码我的PHP发送表单。 代码有什么问题?我已经在年底$头添加的内容类型...这是不是整个文件,也有HTML的PHP​​之后,却没有让我将它张贴编码PHP以其他语言发送邮件

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Contact Form..."; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form your submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form your submitted.');  
    } 

    $name = $_POST['name']; // required 
    $email_from = $_POST['email']; // required 
    $comments = $_POST['comments']; // required 




    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Name: ".clean_string($name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 


// create email headers 
$headers .= 'Content-type: text/plain; charset=windows-1251' . "\r\n"; 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); ;} 
?> 
+0

你想做什么?要让PHP自动翻译你的电子邮件吗? – vindia 2012-01-27 09:57:10

回答

0

尝试使用第三方库,喜欢的PHPMailer:

例如:http://phpmailer.worxware.com/index.php?pg=exampleamail 下载:http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/

不要忘记设置字符集,这样的:

<?php 
require_once '../class.phpmailer.php'; 

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 
$mail->CharSet = 'utf-8'; 

try { 
    $mail->AddReplyTo('[email protected]', 'First Last'); 
    $mail->AddAddress('[email protected]', 'John Doe'); 
    $mail->SetFrom('[email protected]', 'First Last'); 
    $mail->AddReplyTo('[email protected]', 'First Last'); 
    $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; 
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically 
    $mail->MsgHTML(file_get_contents('contents.html')); 
    $mail->AddAttachment('images/phpmailer.gif');  // attachment 
    $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment 
    $mail->Send(); 
    echo "Message Sent OK\n"; 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 
?> 
+0

谢谢。上帝祝福你。它没有这样工作,我不知道发生了什么。我是PHP初学者。谢谢你的帮助人。 – 2012-01-27 11:14:37

+0

我们每个人都是初学者,我知道这很令人沮丧:) 但是使用@Sudhir的简单例子。在你的问题中,你只需要将编码更改为utf-8 – machineaddict 2012-01-28 08:01:33

1

对于发送邮件在不同的语言,你可以改变的字符集:

 

$headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n"; 
$headers .= 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
 

而且可以肯定的页面编码为UTF-8,如果使用一个数据库,表(或整个数据库)是“UTF-8 unicode general“

使用UTF-8,您可以在出现字符时编写字符,不要使用实体。

你的意思是这样的。希望它可以帮助

+0

谢谢你。上帝祝福你。它没有这样工作,我不知道发生了什么。感谢您的帮助 ;)。 – 2012-01-27 11:16:49

0

请尝试以下操作:

将数据库/表/行的排序规则设置为UTF-8。 UTF8_general_ci应该这样做。 将MySQL和PHP之间的连接设置为UTF-8。 (通过连接或通过设置默认连接编码执行查询SET NAMES'utf8')。 尝试使用PHP发送内容类型标头:header("Content-Type: text/html; charset=utf-8");

+0

谢谢。上帝祝福你。它没有这样工作,我不知道发生了什么。我是PHP初学者。谢谢你的帮助人。 – 2012-01-27 11:16:59