WordPress 2.2.3 のコメント通知メールの文字コードを変更する

WordPress 2.2.3 のコメント通知メールは、本体で使用している文字コードと同じもので送られてきます。ウチの場合は UTF-8 なので、通常のメーラ ((Gmail とか Becky!2 とか。今は Google Apss にメールはお任せしてるので、Gmail 一本です。)) での内容確認は問題ないのですが、携帯電話に転送すると UTF-8 が利用できず、化け化けになってしまうのを何とかしたいと思っていました。

ということで、検索。 WordPress 2.2.3のコメント通知メールをISO-2022-JPに | 老眼palm というエントリを発見し、そのパッチを適用してみたところ、件名および本文の文字化けは解消。
件名の方は、時折一部が化ける現象が発生していたのですが、メールの件名の文字化け防止パッチの実証プラグイン « iDeasilo を導入することで、それも解消。数件テストしてみた感じでは、件名の化けも起きていないようです。

で、気づいたのが差出人 From: フィールドは化けたままだということ。なので、さらにちょちょいと1行書き換えてテストしたら化けなかったのでこれでいいか、と。

パッチ

./wp-includes/pluggable.php に対するパッチは、以下。老眼palm のものとほぼ同じですが、$phpmailer->FromName についても、mb_encode_mimeheader を通してみました。

$ diff -u pluggable.php.bak pluggable.php
--- pluggable.php.bak   2007-09-08 14:06:51.000000000 +0900
+++ pluggable.php       2007-09-20 00:19:33.000000000 +0900
@@ -160,6 +160,8 @@
 function wp_mail($to, $subject, $message, $headers = '') {
        global $phpmailer;
 
+       mb_internal_encoding("UTF-8");
+
        if ( !is_object( $phpmailer ) ) {
                require_once(ABSPATH . WPINC . '/class-phpmailer.php');
                require_once(ABSPATH . WPINC . '/class-smtp.php');
@@ -185,8 +187,12 @@
 
        $phpmailer->FromName = "WordPress";
        $phpmailer->AddAddress("$to", "");
-       $phpmailer->Subject = $subject;
-       $phpmailer->Body    = $message;
+//     $phpmailer->Subject = $subject;
+//     $phpmailer->Body    = $message;
+       $phpmailer->CharSet = "ISO-2022-JP";
+       $phpmailer->Encoding = "7bit";
+       $phpmailer->Subject = mb_encode_mimeheader($subject, "ISO-2022-JP", "B", "\n");
+       $phpmailer->Body    = mb_convert_encoding($message, "ISO-2022-JP", "UTF-8");
        $phpmailer->IsHTML(false);
        $phpmailer->IsMail(); // set mailer to use php mail()
 
@@ -197,14 +203,16 @@
                $header = explode( ":", $line );
                switch ( trim( $header[0] ) ) {
                        case "From":
+                               
                                $from = trim( str_replace( '"', '', $header[1] ) );
                                if ( strpos( $from, '<' ) ) {
-                                       $phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
+                                       $phpmailer->FromName = mb_encode_mimeheader(str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) ), "ISO-2022-JP", "B", "\n");
                                        $from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
                                        $from = str_replace( '>', '', $from );
                                } else {
                                        $phpmailer->FromName = $from;
                                }
+                               
                                $phpmailer->From = trim( $from );
                                break;
                        default: