本文介绍了PHP IMAP解码消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过base64编码和8bit编码发送的电子邮件.我想知道如何使用imap_fetchstructure检查消息的编码(因为这样做大约两个小时,所以丢失了),然后对其进行正确解码.

I have emails sent via base64 encoding and 8bit encoding. I was wondering how I could check the encoding of the message using imap_fetchstructure (been doing this for about two hours, so lost) and then decode it properly.

Gmail和Mailbox(iOS上的应用程序)将其作为8bit发送,而Windows 8的Mail应用程序将其作为base64发送.无论哪种方式,我都需要通过检测使用的是哪种编码方式来对其8bit或base64进行解码.

Gmail and Mailbox (app on iOS) are sending it as 8bit while Windows 8's Mail app is sending it as base64. Either way, I need to decode whether its 8bit or base64 by detecting what type of encoding it has used.

使用PHP 5.1.6(是的,我应该更新,很忙).

Using PHP 5.1.6 (yes, I should update, been busy).

我真的没有代码要显示.这就是我所拥有的:

I really have no code to show. This is all I have:

<?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);
        $message = imap_fetchbody($inbox,$email_number,2);
        $struct = imap_fetchstructure($inbox, $email_number);

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

        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
    }

    echo $output;
} 

imap_close($inbox);
?>

推荐答案

imap_bodystruct()或imap_fetchstructure()应该将此信息返回给您.以下代码应完全满足您的需求:

imap_bodystruct() or imap_fetchstructure() should return this info to you. The following code should do exactly what you're looking for:

<?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);
?>

这篇关于PHP IMAP解码消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 15:57