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


Encoding PHP to send mails in other languege

我想用我的语言对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:

示例: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('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_general_ci应该这样做。将MySQL和PHP之间的连接设置为UTF-8。(通过在连接后执行查询SET NAMES'utf8'或通过设置默认连接编码)。尝试使用PHP:header("Content-Type: text/html; charset=utf-8");发送内容类型标头。