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条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大