imap_delete not working
Asked Answered
J

2

7

I am using php imap functions to parse the message from webmail. I can fetch messages one by one and save them in DB. After saving, I want to delete the inbox message. imap_delete function is not working here. My code is like that:

$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);

I also tested - imap_expunge($connection);
But it is not working. The messages are not deleted. Please help me out...

Juno answered 4/9, 2010 at 5:13 Comment(0)
A
10

You are mixing POP and IMAP.

That is not going to work. You need to open the connection with IMAP. See this example:

<?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);
?>
Affixation answered 4/9, 2010 at 5:17 Comment(3)
Please note shamittomar's use of imap_expunge AFTER the deletion. In IMAP you first mark the messages for deletion, and when you're done marking them you do one expunge call to finally remove the marked messages.Unionize
Actually the functions names are like pop3. but they perform imap functionality.Juno
@Emrul, did you try this code? Otherwise please paste the whole source code in your question.Affixation
J
0

Actually the functions names are like pop3. but they perform imap functionality. like -

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;
} 
Juno answered 4/9, 2010 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.