douhao2548 2014-04-03 13:33
浏览 86
已采纳

PHP电子邮件正文解码

after searching and trying solutions here for quite some time I couldn't find a fix for my problem, although it did fix one of the messages..

Anyways to the point, im currently using the imap function in PHP to get Gmail e-mails and so far it worked with 2 messages, but the other messages are still encoded or wrongly decoded and I can't find where I did something wrong, my current code:

<?php

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '******@*******.com';
$password = '*******';


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


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

$max = 5;
$i = 0;

if($emails) {

    $output = '';

    rsort($emails);

    foreach($emails as $email_number) {

        if($i === $max) {
            break;
        }

        $overview = imap_fetch_overview($inbox,$email_number, 0);
        $structure = imap_fetchstructure($inbox, $email_number);

        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        if (isset($overview[0]->subject)) {
            $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        }else{
            $output.= '<span class="subject">No subject</span> ';
        }
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        $message = imap_fetchbody($inbox, $email_number, 2);

        if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
            $part = $structure->parts[1];
            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="body">'.utf8_encode($message).'</div>';
        $i++;
    }

    echo $output;
} 

imap_close($inbox);

?>

Output: http://www.mupload.nl/img/7pn51ldwpxj.png

Thanks alot upfront!

  • 写回答

2条回答 默认 最新

  • dpp80346 2014-04-03 16:31
    关注

    You've likely selected the wrong body part for those messages. Those are binary files: one is a GIF file (see the GIF89 header), and the other is a ZIP file (see the PK header).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答