15

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

Gmail 和邮箱(iOS 上的应用程序)以 8 位发送,而 Windows 8 的邮件应用程序以 base64 发送。无论哪种方式,我都需要通过检测它使用的编码类型来解码它是 8bit 还是 base64。

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

我真的没有代码可以显示。这就是我所拥有的:

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

4 回答 4

47

imap_bodystruct() 或 imap_fetchstructure() 应该将此信息返回给您。以下代码应该完全符合您的要求:

<?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);
?>
于 2013-03-28T17:08:48.093 回答
10

你可以看看这个例子。

地图/地图

这是代码片段

switch ($encoding) {
    # 7BIT
    case 0:
        return $text;
    # 8BIT
    case 1:
        return quoted_printable_decode(imap_8bit($text));
    # BINARY
    case 2:
        return imap_binary($text);
    # BASE64
    case 3:
        return imap_base64($text);
    # QUOTED-PRINTABLE
    case 4:
        return quoted_printable_decode($text);
    # OTHER
    case 5:
        return $text;
    # UNKNOWN
    default:
        return $text;
}
于 2015-03-03T13:30:11.197 回答
2

imap_fetchstructure() 的返回对象

  1. 编码(体传输编码)

传输编码(可能因​​使用的库而异)

0 7 位 1 8 位 2 二进制 3 BASE64 4 引用可打印 5 其他

$s = imap_fetchstructure($mbox,$mid);
if ($s->encoding==3)
    $data = base64_decode($data);
于 2013-04-02T11:21:24.360 回答
-1
            // Best for reading attached file from e-mail as well as body of the mail
            $hostname = '{imap.gmail.com:993/imap/ssl}Inbox';
            $username = 'xzy.xyz@xyz.com';
            $password = '****************';

            $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());  
            $emails   = imap_search($inbox, 'SUBJECT "'.$subject.'"');
            rsort($emails);
            foreach ($emails as $key => $value) {

                $overview = imap_fetch_overview($inbox,$value,0);
                $message_date = new DateTime($overview[0]->date);
                $date = $message_date->format('Ymd');
                $message = imap_fetchbody($inbox,$value,2);
                $structure = imap_fetchstructure($inbox, $value);
                $attachments = [];
                if(isset($structure->parts) && count($structure->parts)) 
                {
                    for($i = 0; $i < count($structure->parts); $i++) 
                    {
                        $attachments[$i] = array(
                                'is_attachment' => false,
                                'filename' => '',
                                'name' => '',
                                'attachment' => ''
                            );
                        if($structure->parts[$i]->ifdparameters) 
                        {
                            foreach($structure->parts[$i]->dparameters as $object) 
                            {
                                if(strtolower($object->attribute) == 'filename') 
                                {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['filename'] = $object->value;
                                }
                            }
                        }

                        if($structure->parts[$i]->ifparameters) 
                        {
                            foreach($structure->parts[$i]->parameters as $object) 
                            {
                                if(strtolower($object->attribute) == 'name') 
                                {
                                    $attachments[$i]['is_attachment'] = true;
                                    $attachments[$i]['name'] = $object->value;
                                }
                            }
                        }

                        if($attachments[$i]['is_attachment']) 
                        {
                            $attachments[$i]['attachment'] = imap_fetchbody($inbox, $value, $i+1);                           
                            if($structure->parts[$i]->encoding == 3) //3 = BASE64 encoding
                            { 
                                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                            }
                            elseif($structure->parts[$i]->encoding == 4) //4 = QUOTED-PRINTABLE encoding
                            { 
                                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                            }
                        }
                    }
                }
            }
            imap_close($inbox);//Never forget to close the connection
于 2019-05-31T16:17:43.280 回答