如何使用php imap将邮件消息移动到文件夹

时间:2011-08-02 22:43:30

标签: php imap

我似乎无法将邮件移至我保存的文件夹中。这是我的代码:

$mbox = imap_open("{".$mail_server.":".$mail_port."}".$mail_folder,            
$mail_username, $mail_password) or die("Error opening mailbox: ".imap_last_error());

$countnum = imap_num_msg($mbox);
$msglist = array();

if( $countnum > 0 ) {
$num = 1;


while ($num <= $countnum) {

$msglist[] = $num;
$num++;

     }//end while loop

}

//move the email to our saved folder
imap_mail_move($mbox,implode(',',$msglist),'INBOX/Saved');
imap_expunge($mbox);
imap_close($mbox);

当我运行此脚本时,没有任何反应。邮件保留在收件箱中。有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:6)

通过查看imap-mail-move()的文档,我发现你已将你的范围与,以及从1开始计算,因此不需要for循环:

<?php 
$mbox = imap_open("{".$mail_server.":".$mail_port."}INBOX", $mail_username, $mail_password) or die("Error opening mailbox: ".imap_last_error());

$countnum = imap_num_msg($mbox);

if($countnum > 0) {
    //move the email to our saved folder
    $imapresult=imap_mail_move($mbox,'1:'.$countnum,'INBOX/Saved');
    if($imapresult==false){die(imap_last_error());}
    imap_close($mbox,CL_EXPUNGE);
}
?>