dtdr57046 2010-12-10 09:48 采纳率: 100%
浏览 50
已采纳

如何从PHP中删除Maildir中的电子邮件?

I'm going crazy with a little problem with Maildir and PHP. I need to check the APACHE_RUN_USER's Maildir and parse delivery-status messages.

The problem removing message after reading; i noticed that Zend_Mail_Storage_Maildir->removeMessage() is still a stub.

try {   
    $mailbox = new Zend_Mail_Storage_Maildir( array('dirname' =>    '/home/' . $_ENV['APACHE_RUN_USER']  . '/Maildir/') );

    foreach ($mailbox as $id => $message) {

        // seen flag
        if ($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) { continue; }

        //get the unique id
        $uniqueid = $mailbox->getUniqueId($id); 

        //obtain message headers
        $headers = $message->getHeaders();

        //check if the original message was sent from this app and is a delivery-status
        $result = strpos($message, $id_header);
        if($result === false) { echo '1 mail skipped: ' . $uniqueid . '. <br />'; continue; }

        $result = strpos($headers['content-type'], 'delivery-status');
        //if no skip to the next mail
        if($result === false) { echo '1 mail skipped: ' . $uniqueid . '. <br />'; continue; }

        // if everything it's ok process it.

        // clear results
        $data = array();
        // foreach line of message
        foreach( preg_split('/(?
)/', $message) as $line ){
            //clear results
            $matches = array();

            //perform matches on textlines
            if( preg_match('/^(.+)\:\s{0,1}(.+)$/', $line, $matches) ) {
                //grab intrested headers
                foreach( array('Action', 'Status', 'Remote-MTA', 'Diagnostic-Code', $id_header) as $header) {
                    if($matches[1] == $header) $data[$header] = $matches[2];
                }
            }
        }

        // *** I NEED TO DROP THE MESSAGE HERE ***

            // not working code ***
        $currentmessageid = $mailbox->getNumberByUniqueId($uniqueid);
        $mailbox->removeMessage($currentmessageid); 

        // *** I NEED TO DROP THE MESSAGE HERE ***


    // print out results
        echo '<pre class="email">';
        print_r( $data );
        echo '</pre>';  
    }

} catch (Exception $e) {
    echo $e;
}

How can I remove it by hand? Some workarounds?

Thanks.

  • 写回答

2条回答 默认 最新

  • doubutao6216 2010-12-10 13:29
    关注

    In order of tawfekov answer I solved as follow:

    Opening mailbox:

    $mailbox = new Zend_Mail_Storage_Writable_Maildir( array('dirname' =>   '/home/' . $_ENV['APACHE_RUN_USER']  . '/Maildir/') );
    

    Processing mail code:

        foreach ($mailbox as $id => $message) {
            $uniqueid = $mailbox->getUniqueId($id);
    
            /* ... mail processing code ... */
    
            // mark as read
            $currentmessageid = $mailbox->getNumberByUniqueId($uniqueid);
            $mailbox->setFlags($currentmessageid, array(Zend_Mail_Storage::FLAG_SEEN));
    
            // or uncomment to delete it
            //$mailbox->removeMessage($currentmessageid);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算