PHP 通过 IMAP 获取邮件附件

问题描述 投票:0回答:1

与 IMAP 的连接有效,我可以按名称列出附件,但是当我尝试将它们保存在服务器上时,这就是它出现故障的地方。

我尝试了下面的代码,但不知何故我保存的文件是 0 字节......这里有什么问题? 我确实将文件放在与脚本相同的文件夹中,因此我可以稍后重命名它,但我想在其中查看完整的附件。 我已经搜索了其他几个问题,但没有一个真正给我一个可靠的答案。

<?php
$mbox = imap_open("{imap.server.be:993/imap/ssl}INBOX", "user", "pass")
     or die("can't connect: " . imap_last_error());

$MC = imap_check($mbox);

// get information about the current mailbox (INBOX in this case)
$mboxCheck = imap_check($mbox);

// get the total amount of messages
$totalMessages = $mboxCheck->Nmsgs;

// select how many messages you want to see
$showMessages = 5;

// get those messages    
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));

// iterate trough those messages
foreach ($result as $mail) {
    $mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');
    
    if(trim($mailBody)=="") {
        $mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
    }

    echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
    
    $structure = imap_fetchstructure($mbox, $mail->msgno);
    
    $attachments = array();
        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($imap, $m, $i+1);
                    if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        foreach ($attachments as $key => $attachment) {
            $name = $attachment['name'];
            $contents = $attachment['attachment'];
            file_put_contents($name, $contents);
        }   
     
}

imap_close($mbox);
?>
php imap email-attachments
1个回答
0
投票

问题是由于在获取附件内容时在

$imap
函数中使用了
imap_fetchbody()
变量引起的。应该是
$mbox
而不是
$imap
。这是更正后的代码:

<?php
$mbox = imap_open("{imap.server.be:993/imap/ssl}INBOX", "user", "pass")
     or die("can't connect: " . imap_last_error());

$MC = imap_check($mbox);

$mboxCheck = imap_check($mbox);
$totalMessages = $mboxCheck->Nmsgs;
$showMessages = 5;
$result = array_reverse(imap_fetch_overview($mbox,($totalMessages-$showMessages+1).":".$totalMessages));

foreach ($result as $mail) {
    $mailBody = imap_fetchbody($mbox, $mail->msgno, '1.1');
    
    if(trim($mailBody)=="") {
        $mailBody = imap_fetchbody($mbox, $mail->msgno, '1');
    }

    echo nl2br(htmlentities(quoted_printable_decode($mailBody)));
    
    $structure = imap_fetchstructure($mbox, $mail->msgno);
    
    $attachments = array();
        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($mbox, $mail->msgno, $i+1); // Change this line
                    if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        foreach ($attachments as $key => $attachment) {
            $name = $attachment['name'];
            $contents = $attachment['attachment'];
            file_put_contents($name, $contents);
        }   
     
}

imap_close($mbox);
?>

此更改应该可以解决问题,您应该能够以正确的文件大小将附件保存到服务器。

© www.soinside.com 2019 - 2024. All rights reserved.