PHP类ImapMailbox未下载附件

PHP类ImapMailbox未下载附件,php,class,email,imap,Php,Class,Email,Imap,在Windows8上使用PHP5.3运行Apache2.2。试图让PHP类下载附件,但每次我使用getMail()时,每个包含附件的电子邮件上的attachments值都为空 所有其他电子邮件信息都已正确下载 我已经查看了类代码,但无法确定问题可能在哪里 这是我目前的代码: $mailbox = new ImapMailbox('{testsite.com:110/pop3/novalidate-cert}INBOX', 'testemail@testsite.com', 'MyPaSs', A

在Windows8上使用PHP5.3运行Apache2.2。试图让PHP类下载附件,但每次我使用getMail()时,每个包含附件的电子邮件上的attachments值都为空

所有其他电子邮件信息都已正确下载

我已经查看了类代码,但无法确定问题可能在哪里

这是我目前的代码:

$mailbox = new ImapMailbox('{testsite.com:110/pop3/novalidate-cert}INBOX', 'testemail@testsite.com', 'MyPaSs', ATTACH_DIR, 'utf-8');

$mails = array();

foreach($mailbox->searchMailbox('SUBJECT "test attach" SINCE "' . date('m/d/Y', strtotime('-1 week')) . '"') as $mailId) {
        $mail = $mailbox->getMail($mailId);
       $mails[] = $mail;
}
在getMail()中转储$data变量后,似乎有winmail.dat格式的附件。由于“ifid”值为空,因此未分配attachmentId值,因此代码无法访问这些。可以对winmail.dat附件进行解码,但只有在检测到附件并将其写入文件时才能进行解码


有没有关于如何在ImapMailbox代码中创建解决方案的想法?

以下是我所写的解决此问题的内容

在initMailPart()方法的开头,添加以下内容:

static $altAttachmentId = 0;
IF($this->attachmentsDir){
的IF块末尾,在结束
}
括号中添加以下内容:

} elseif (!empty($params['fileName']) || !empty($params['filename']) || !empty($params['name'])) { // Process attachments that are not inline.
                            // Check if need to decode TNEF (Winmail.dat) file.
                            if ($partStructure->ifsubtype && $partStructure->subtype == 'MS-TNEF') {
                                require_once 'path_to_your/tnef_decoder.php';

                                $Tnef = new tnef_decoder;

                                $un_tnef = $Tnef->decompress($data);


                                $attached_files = array();

                                foreach ($un_tnef as $f) {
                                    if (!empty($f['name']) && !empty($f['stream'])) {
                                        $attachment = new IncomingMailAttachment();

                                        $attachment->id = $altAttachmentId;
                                        $attachment->name = $f['name'];
                                        $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $f['name']);

                                        $mail->addAttachment($attachment);

                                        if (file_exists($attachment->filePath) && md5($f['stream']) != md5_file($attachment->filePath)) {
                                            $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $f['name']);
                                        }

                                        file_put_contents($attachment->filePath, $f['stream']);

                                        $altAttachmentId++;
                                    }
                                }
                            } else {
                                if (!empty($params['filename'])) {
                                    $fileName = $params['filename']; // Account for random camel-case mistake on element.
                                } elseif (!empty($params['fileName'])) {
                                    $fileName = $params['fileName'];
                                } else {
                                    $fileName = $params['name'];
                                }

                                $attachment = new IncomingMailAttachment();
                                $attachment->id = $altAttachmentId;
                                $attachment->name = $fileName;
                                $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $fileName);

                                $mail->addAttachment($attachment);

                                file_put_contents($attachment->filePath, $data);

                                $altAttachmentId++;
                            }
                        }
请注意,您必须包含在包中找到的tnef_decoder.php文件,tnef解码才能工作。我得到了TNEF解决方案的灵感

此修改将处理Winmail.dat文件中的所有TNEF编码文件以及未内联附加的任何其他附件。观察大文件上的内存使用情况


如果名称不完全相同,它也不会覆盖相同名称的现有文件。

以下是我编写的修复此问题的内容

在initMailPart()方法的开头,添加以下内容:

static $altAttachmentId = 0;
IF($this->attachmentsDir){
的IF块末尾,在结束
}
括号中添加以下内容:

} elseif (!empty($params['fileName']) || !empty($params['filename']) || !empty($params['name'])) { // Process attachments that are not inline.
                            // Check if need to decode TNEF (Winmail.dat) file.
                            if ($partStructure->ifsubtype && $partStructure->subtype == 'MS-TNEF') {
                                require_once 'path_to_your/tnef_decoder.php';

                                $Tnef = new tnef_decoder;

                                $un_tnef = $Tnef->decompress($data);


                                $attached_files = array();

                                foreach ($un_tnef as $f) {
                                    if (!empty($f['name']) && !empty($f['stream'])) {
                                        $attachment = new IncomingMailAttachment();

                                        $attachment->id = $altAttachmentId;
                                        $attachment->name = $f['name'];
                                        $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $f['name']);

                                        $mail->addAttachment($attachment);

                                        if (file_exists($attachment->filePath) && md5($f['stream']) != md5_file($attachment->filePath)) {
                                            $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $f['name']);
                                        }

                                        file_put_contents($attachment->filePath, $f['stream']);

                                        $altAttachmentId++;
                                    }
                                }
                            } else {
                                if (!empty($params['filename'])) {
                                    $fileName = $params['filename']; // Account for random camel-case mistake on element.
                                } elseif (!empty($params['fileName'])) {
                                    $fileName = $params['fileName'];
                                } else {
                                    $fileName = $params['name'];
                                }

                                $attachment = new IncomingMailAttachment();
                                $attachment->id = $altAttachmentId;
                                $attachment->name = $fileName;
                                $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $fileName);

                                $mail->addAttachment($attachment);

                                file_put_contents($attachment->filePath, $data);

                                $altAttachmentId++;
                            }
                        }
请注意,您必须包含在包中找到的tnef_decoder.php文件,tnef解码才能工作。我得到了TNEF解决方案的灵感

此修改将处理Winmail.dat文件中的所有TNEF编码文件以及未内联附加的任何其他附件。观察大文件上的内存使用情况


如果名称不完全相同,它也不会覆盖相同名称的现有文件。

我相信您实际上必须按字节读取附件中的文件并将其写入新文件。.我相信您实际上必须按字节读取附件中的文件并将其写入新文件。。