如何从php中的电子邮件中附件中检索附加邮件?

时间:2015-03-30 04:44:45

标签: php email imap

我使用以下代码使用imap_fetchstructure从电子邮件中检索附件。该代码适用于普通附件,如图像,文件,但不适用于Outlook项目。请参阅以下代码:

        $this->msg_cnt = imap_num_msg($this->conn);
        $this->TOTAL_MESSAGES = $this->msg_cnt;

        $msgs = imap_sort($this->conn, SORTDATE, 1, SE_UID);

        $in = array();
        $inctr = 0;
        foreach ($msgs as $msguid)
        {
            $msgno = imap_msgno($this->conn, $msguid);
            $has_attachment = false;
            $ATT_FILES = "";
            $attachment_value = "";
            $file_name = "";

            $structure = imap_fetchstructure($this->conn, $msgno);
           $attachments = array();
           $log = "Attachments:\r\n\r\n";
            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'])
                    {
                        $has_attachment = true;
                        $attachments[$i]['attachment'] = imap_fetchbody($this->conn, $msgno, $i+1);
                        if($structure->parts[$i]->encoding == 3)
                        { // 3 = BASE64
                            $result = "Found file attachment (QUOTED-PRINTABLE).\r\n\r\n";
                            file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);
                            $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                        }
                        elseif($structure->parts[$i]->encoding == 4)
                        { // 4 = QUOTED-PRINTABLE
                            ob_start();
                            echo "QUOTED-PRINTED FOUND. CONTENT:\r\n";
                            var_dump($attachments[$i]);
                            $result = ob_get_clean();
                            file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);
                            $result = "Found email attachment (QUOTED-PRINTABLE).\r\n\r\n";
                            file_put_contents("/var/www/html/attachment_logs.txt", $result, FILE_APPEND);

                            $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                        }
                        $attachment_value = $attachments[$i]['attachment'];
                        if (empty($attachments[$i]['filename']))
                            $attachments[$i]['filename'] = "".time()."";
                        $file_name = $attachments[$i]['filename'];
                        $folder = "/var/www/html/dvxcss/attachments/";
                        if (!file_exists($folder))
                        {
                            mkdir($folder);
                        }

                        //WRITE THE ATTACHMENT
                        ob_start();
                        var_dump($attachments);
                        $log .= ob_get_clean();
                        $log .= "\r\n\r\n#################################\r\n";
                        file_put_contents("/var/www/html/attachment_logs.txt", $log, FILE_APPEND);
                        $ATT_FILES .= $file_name.";";
                        file_put_contents($folder.$file_name, $attachment_value);
                    }             
                }
            }

            $in[$inctr] = array (
                'index'     => $inctr,
                'header'    => imap_headerinfo($this->conn, $msgno),
                'body' => $this->getBody($msguid, $this->conn),
                'structure' => imap_fetchstructure($this->conn, $msgno),
                'hasattachment' => $has_attachment,
                'attachment' => "$file_name",
                'attfiles' => $ATT_FILES
            );
            $inctr++;
        }

代码获取附件的文件名并将其写入服务器。

问题:

如何使用php获取Outlook的电子邮件附件?

任何帮助都将非常感激。谢谢!

更新

发现它们是.eml文件。这些项目由outlook附加为.eml文件。我尝试使用gmail进行检查,并获得一个名为&#34; noname.eml&#34;的文件。

更新

IMAP- Parsing original headers from a bounced mail

它被称为 message / rfc822 text / rfc822 bodypart。

0 个答案:

没有答案