drfu80954 2017-01-05 15:50
浏览 32

无法在PHP IMAP电子邮件提取中解密比特流

I'm using PHP's IMAP functions to scrape emails out of a Gmail account, using pretty standard code. It seems to work with text and HTML emails, but NEVER with any email with an attachment or embedded image.

Whenever there's an attachment, the return value of imap_fetchbody() is just one big bitstream rendered in ASCII--and there's no part that looks anything like the original message, so I can't imagine any way to extract the original message without the attachments. What's going on here?

Here is my code:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'XXXX';
$password = 'XXXX';
$good = 0;
$max_emails = 25;
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to     Gmail: ' . imap_last_error());
$datelimit = date ( "d M Y", strToTime ( "-1 days" ) );
$emails = imap_search($inbox,"ALL");
if($emails) {
    $output = '';
    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);
            }
        }    
        echo "Message: " . $message . "<br><br>";
...

I appreciate any insights as to what is missing that would cause it to fail to read an email. Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离