2010-09-04 218 views
3

我正在使用php imap函數解析來自webmail的消息。我可以一個一個獲取消息並將它們保存在數據庫中。保存後,我想刪除收件箱信息。 imap_delete函數在這裏不起作用。我的代碼是這樣的:imap_delete無法正常工作

$connection = pop3_login($host,$port,$user,$pass,$folder="INBOX",$ssl=false);//connect 
$stat = pop3_list($connection);//list messages 

foreach($stat as $line) { 
    //save in db codes... 
    imap_delete($connection, $line['msgno']);//flag as delete 
} 

imap_close($connection, CL_EXPUNGE); 

我還測試 - imap_expunge($connection);
但它無法正常工作。消息不會被刪除。請幫助我...

回答

6

您正在混合使用POP和IMAP。

這是行不通的。您需要打開與IMAP的連接。看到這個例子:

<?php 

$mbox = imap_open("{imap.example.org}INBOX", "username", "password") 
    or die("Can't connect: " . imap_last_error()); 

$check = imap_mailboxmsginfo($mbox); 
echo "Messages before delete: " . $check->Nmsgs . "<br />\n"; 

imap_delete($mbox, 1); 

$check = imap_mailboxmsginfo($mbox); 
echo "Messages after delete: " . $check->Nmsgs . "<br />\n"; 

imap_expunge($mbox); 

$check = imap_mailboxmsginfo($mbox); 
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n"; 

imap_close($mbox); 
?> 
+3

請注意shamittomar在刪除之後使用imap_expunge。在IMAP中,您首先標記要刪除的郵件,當您完成標記時,您會執行一次刪除呼叫以最終刪除標記的郵件。 – 2010-09-04 05:44:41

+0

其實函數名稱就像pop3。但他們執行imap功能。 – 2010-09-04 06:39:31

+0

@Emrul,你試過這段代碼嗎?否則,請在您的問題中粘貼整個源代碼。 – shamittomar 2010-09-04 06:59:00

0

其實功能名稱就像pop3。但他們執行imap功能。像 -

function pop3_login($host,$port,$user,$pass,$folder="INBOX",$ssl=false) 
{ 
    $ssl=($ssl==false)?"/novalidate-cert":""; 
    return (imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass)); 
} 
function pop3_list($connection,$message="") 
{ 
    if ($message) 
    { 
     $range=$message; 
    } else { 
     $MC = imap_check($connection); 
     $range = "1:".$MC->Nmsgs; 
    } 
    $response = imap_fetch_overview($connection,$range); 
    foreach ($response as $msg) $result[$msg->msgno]=(array)$msg; 
     return $result; 
}