【问题标题】:How do you get an attached PDF from an e-mail and save it, php您如何从电子邮件中获取附加的 PDF 并保存,php
【发布时间】:2013-02-02 07:19:43
【问题描述】:

我已经坚持了好几个小时了。但基本上,我有一个收件箱,其中包含一些我想取出的内容和附件 PDF。我想出了如何取出我想要的所有 HTML,但是如何保存附加的 PDF?

这是我目前拥有的鳕鱼:

公共函数 read_emails(){ // 设置用户检查

    $strUser     = "email";
    $strPassword = "passwrd";
    $delete_emails=false;

    // open
    $hMail = imap_open ("{webmail.somethingsomething.com:143/notls}INBOX", "$strUser", "$strPassword") or die("can't connect: " . imap_last_error());
    // get headers
    $aHeaders = imap_headers( $hMail );
    // get message count
    $objMail = imap_mailboxmsginfo( $hMail );
    if($objMail != NULL)
    {           
        // process messages
        for( $idxMsg = 1; $idxMsg <= $objMail->Nmsgs; $idxMsg++  )
        {
            // get header info
            $objHeader = imap_headerinfo( $hMail, $idxMsg );

            // get from object array
            $aFrom = $objHeader->from;

            // process headers
            for( $idx = 0; $idx < count($aFrom); $idx++ )
            {
                // get object
                $objData = $aFrom[ $idx ];
                // get email from
                $strEmailFrom = $objData->mailbox . "@" . $objData->host;
                //Get the subject 
                $message_subject = $objHeader->subject;
                //Get the body of the message
                $fullBody = imap_fetchbody($hMail,$idxMsg, 2);//displays full body including junk
                $bodyMessage = quoted_printable_decode($fullBody);
                //Get the msg structure 
                $message_structure = imap_fetchstructure($hMail, $idx);
                //WHAT DO I DO HERE????
            }

            // delete message
            if($delete_emails == true){
                imap_delete( $hMail, $idxMsg );
            }

        }

        // expunge deleted messages
        if($delete_emails == true){
            imap_expunge( $hMail );
        }


        //Clears the cache
        imap_gc($hMail, IMAP_GC_ELT);

    }
    // close
    imap_close( $hMail );



}

我有消息结构,如果我 print_r 可以看到其中一个部分有附件,我可以看到它是 PDF。不知道接下来会发生什么。我已经尝试过,似乎无法找到一个好的解决方案。

谢谢!

编辑:这里是 imap_fetchstructure 的内容

stdClass 对象 ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => MIXED [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters ] => 0 [ifparameters] => 1 [parameters] => Array ([0] => stdClass Object ([attribute] =>boundary [value] => f46d044473ef81609f04d5f1997c) ) [parts] => Array ([0] = > stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => ALTERNATIVE [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] = > 0 [ifparameters] => 1 [parameters] => Array ([0] => stdClass Object ([attribute] => boundary [value] => f46d044473ef81609b04d5f1997a)) [parts] => Array ([0] => stdClass对象 ( [type] => 0 [encoding] => 0 [ifsubtype] => 1 [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 37 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 数组([0] => stdClass 对象([attribute] => charset [value] => ISO-8859-1) ) ) [1] => stdClass Object ( [type] => 0 [encoding] => 0 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 37 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 数组 ([0] => stdClass 对象 ([attribute] => charset [value] => ISO-8859-1 ) ) ) ) [1] => stdClass 对象( [type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => PDF [ifdescription] => 0 [ifid] => 0 [bytes] => 179876 [ifdisposition] => 1 [ disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ([0] => stdClass Object ([attribute] => filename [value] => Statement of Account.pdf) ) [ifparameters] => 1 [parameters] => Array ([0] => stdClass Object([attribute] => name [value] => Statement of Account.pdf ) ) ) )

【问题讨论】:

    标签: php email imap attachment


    【解决方案1】:

    根据doc,您在$message_structure-&gt;parts 中有一个身体部位数组,因此您可以循环该数组并在零件的-&gt;type-&gt;subtype 对应于PDF 文档时停止。比如:

    foreach ($message_structure->parts as $idx => $part) {
        if (TYPEAPPLICATION === $part->type && 'pdf' === strtolower($part->subtype)) {
            $data = imap_fetchbody($hMail, $idxMsg, $idx);
            // Then you should check the $part->encoding
            // to decode data appropriately, e.g.:
            if (ENCBASE64 === $part->encoding) {
                $data = base64_decode($data);
            }
        }
    }
    

    【讨论】:

    • 您不必下载pdf或其他内容并保存吗?你是怎么做到的
    • 我想我必须获取数据并以某种方式对其进行编码?
    • 嗯,好吧...所以我做到了。然后我保存了文件: write_file('static/statements/attachment.pdf', $data); (我正在使用 codeigniters 文件助手)。有一个 pdf 文件被写入,但是当我打开它时,它说:Adobe Reader 无法打开“attachment.pdf”,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件附件发送的,但未正确解码)。
    • 您保存的文件大小是否等于 179876 字节?
    • 文件实际附件大小为1799680,保存的文件大小为0字节
    猜你喜欢
    • 2013-09-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2010-09-12
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多