dpr81047 2011-11-26 11:10
浏览 301
已采纳

如何将邮件从Gmail收件箱移动到标签?

I'm trying to move messages away from Inbox into Processed label with this code:

$inbox = imap_open($host,$user,$pass) or die('Error: ' . imap_last_error());

if( $emails = imap_search($inbox,'ALL') )
{
    foreach($emails as $email_number) {
        imap_mail_move($inbox, $email_number, 'Processed') or die('Error');
    }

}
imap_expunge($inbox);
imap_close($inbox);

Unfortunately, while the messages get the Processed label, they're still left in Inbox too.

How would I make them go away from Inbox?

  • 写回答

5条回答 默认 最新

  • dongzhuo7291 2011-12-06 15:58
    关注

    Actually... The reason why the emails were left in the inbox was that when imap_mail_move did it's thing, the IDs of all the leftover messages got decremented by one, so when the foreach loop moved to the next message, one message was left behind. This skipping a message repeated for every iteration. That's why it seemed that imap_mail_move was not working.

    The solution is to use unique message UIDs instead of potentially repeating IDs:

    $inbox  = imap_open( $host, $user, $pass );
    $emails = imap_search( $inbox, 'ALL', SE_UID );
    
    if( $emails ) {
        foreach( $emails as $email_uid ) {
            imap_mail_move($inbox, $email_uid, 'processed', CP_UID);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)