编码PHP以用其他语言发送邮件

编码PHP以用其他语言发送邮件,php,encoding,Php,Encoding,我想用我的语言对php发送表单进行编码。 代码有什么问题?我在结尾的$headers中添加了内容类型。。。这不是整个文件,还有HTML后的PHP,但没有让我发布它 <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "gancho_lambev@abv.bg"; $email_subject = "Contact Form..."; f

我想用我的语言对php发送表单进行编码。 代码有什么问题?我在结尾的$headers中添加了内容类型。。。这不是整个文件,还有HTML后的PHP,但没有让我发布它

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "gancho_lambev@abv.bg";
    $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);  ;}
?>

对于以不同语言发送邮件,您只需更改字符集即可:


$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,您可以按显示方式写入字符,但不要使用实体


你的意思是这样的吗。希望它有帮助

尝试使用第三方库,如phpmailer:

例如: 下载:

不要忘记设置字符集,如下所示:

<?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('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', '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!
}
?>

尝试以下操作:

将数据库/表/行的排序规则设置为UTF-8<代码>UTF8\u常规\u ci应该可以。 将MySQL和PHP之间的连接设置为
UTF-8
。(通过在连接后执行查询集名称“utf8”或设置默认连接编码)。
尝试使用PHP发送内容类型标题:
标题(“内容类型:text/html;charset=utf-8”)

你想做什么?让php自动为您翻译电子邮件?谢谢。上帝保佑你。事情不是这样的,我不知道发生了什么。我是PHP的初学者。谢谢你的帮助。我们每个人都曾经是初学者,我知道这很令人沮丧:)但是用@Sudhir这个简单的例子。在您的问题中,您只需将编码更改为utf-8谢谢。上帝保佑你。事情不是这样的,我不知道发生了什么。谢谢你的帮助;)。谢谢你。上帝保佑你。事情不是这样的,我不知道发生了什么。我是PHP的初学者。谢谢你的帮助,伙计。