Hi,

I have written a php script which will delete certain emails from Gmail using IMAP protocol. But the script is unable to delete emails.

After I run the script, the emails are removed from the INBOX, but when I click on ALL EMAILS, I see the deleted emails available there. I want the emails to be totally deleted.

I have IMAP, POP3 enabled. All emails were received after the POP3 was enabled.

This is my script:

<?php
//error_reporting(0);

    $mailbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX", "xxx@gmail.com","xxx");

    // Check messages
    $check = imap_check($mailbox);
    print("Number of messages : " . $check->Nmsgs . "\n\n\n");

    for($d=1; $d<=$check->Nmsgs; $d++)
    {
            $header = imap_header($mailbox, $d);
            if ((preg_match('/night/i', $header->Subject)))
            {
                    imap_delete($mailbox, $d);
            }

    }
    $delete = imap_expunge ($mailbox);
    imap_close($mailbox);
    echo "DONE";
?>

Please help, I have tried all possible options and I have researched the net extensively. I am almost ready to give up.

Forum, please help me.

Thx
Vai

    you need to move them to [Gmail]/Trash then they get purged after 30 days or sooner if you set the option.

      Hi,

      Using PHP, how to move emails to [Gmail]/Trash.

      Please help!!

      Thx
      Vai

        Write a Reply...