ITeye博客

PHP mail 中文乱码的解决方式

yeahfeng · 2012-11-06 17:18 · PHP · 编程语言 · Markdown

主要是编码的问题:

解决办法就是: 读取的数据设置好编码方式; 发邮件的时候也指定编码方式;

先用函数base64_encode() 使用 MIME base64 对数据进行编码
标题字符串前加编码类型例如: =?UTF-8?B?
当然如果是gb2312的话就 =?GB2312?B?
标题字符串后加:?=

-----------我的供配置参考---------

  1. <html>
  2. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  3. <body>
  4. <?php
  5. if(strlen($_POST["from"]) > 1)
  6. //if "email" is filled out, send email
  7. {
  8. //send email
  9. $email = $_POST["from"] ;
  10. $mailto = $_POST["mailto"];
  11. $subject = "=?UTF-8?B?" . base64_encode($_POST["subject"]) . "?=";
  12. $message = $_POST["message"] ;
  13. $header = 'MIME-Version: 1.0' . "\r\n"
  14. . "Content-type: text/html; charset=utf-8". "\r\n"
  15. . "From: <$email>"."\r\n";
  16. //echo "mailto:$mailto<br />", "Subject: $subject<br />", "message:$message<br />", "From: $email<br />" ;
  17. mail("$mailto", "$subject", $message, "$header" );
  18. echo "<br />Your mail has been sent !
  19. Thank you for using our Mail system ...<br />
  20. We shall go to main page in 3 seconds <br />";
  21. }
  22. else{
  23. echo "<center><h2>Welcome to Qunero-php Mail system </h2></center><br />";
  24. echo '<form method="post" action="index.php">
  25. From : <input type="text" name="from" /><br/ >
  26. MailTo : <input type="text" name="mailto" /><br />
  27. Subject : <input type="text" name="subject" /><br />
  28. Message : <br /><textarea name="message" rows="15" cols="80"> </textarea><br />
  29. <input type="submit" />
  30. </form>';
  31. }
  32. ?>
  33. </body>
  34. </html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。