How to decode subject and date which were received from gmail (text in Japan)

I have a function using PHP IMAP to receive email from G-mail. the function was going well when everyone returned a message from a gmail account, but there was still a small issue with the subject and the decoding date in Japanese. It was for the decoding in the message that it worked. Therefore, I would like to ask you how can I decode to read from PHP.

Here is my function:

<?php
$hostname = '{********:993/imap/ssl}INBOX';
$username = '*********';
$password = '******';

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());

$emails = imap_search($inbox,'ALL');

if($emails) {
    $output = '';
    rsort($emails);

    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $structure = imap_fetchstructure($inbox, $email_number);

        if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
            $part = $structure->parts[1];
            $message = imap_fetchbody($inbox,$email_number,2);

            if($part->encoding == 3) {
                $message = imap_base64($message);
            } else if($part->encoding == 1) {
                $message = imap_8bit($message);
            } else {
                $message = imap_qprint($message);
            }
        }

        $output.= '<div class="toggle'.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="from">From: '.utf8_decode(imap_utf8($overview[0]->from)).'</span>';
        $output.= '<span class="date">on '.utf8_decode(imap_utf8($overview[0]->date)).'</span>';
        $output.= '<br /><span class="subject">Subject('.$part->encoding.'): '.utf8_decode(imap_utf8($overview[0]->subject)).'</span> ';
        $output.= '</div>';

        $output.= '<div class="body">'.$message.'</div><hr />';
    }

    echo $output;
}

imap_close($inbox);
?>

      

and the result is (wrong):

enter image description here

Can anyone tell me what function in PHP can solve this problem.

Hello,

+3


source to share


1 answer


 public function encodeToUtf8($string) {
        mb_internal_encoding("utf-8");
        $str_japan = mb_convert_encoding($string, "UTF-8", "ISO-2022-JP");
        //"=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?=";
        return mb_decode_mimeheader($str_japan);
}

      

using:



encodeToUtf8($overview[0]->date);
encodeToUtf8($overview[0]->subject);

      

+2


source







All Articles