Jump to content

PHP / IMAP / Gmail : Cannot delete emails


vaibhavs

Recommended Posts

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
        $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.

 

I await some help pls.

 

Thx

Vai

Link to comment
Share on other sites

I'm not 100% familiar with gmail as I rarely use mine but I'd be willing to bet this is a safety measure implemented by them to make sure you don't mistakenly delete all your  mail.  Is is possible to open your "All mail" tab instead of your "Inbox" tab and delete them all from there?

 

Is is possible to delete all mail from "Inbox" and then also delete them from "All mail?"

 

Is there a setting in gmail to NOT keep mail after it has been deleted from your inbox?

 

Hopefully those will give you a few things to work with and hopefully you can get this corrected.  Please post back on here and I'll see if can brain storm a little more.  I'm also gonna mess with my gmail for a bit here

Link to comment
Share on other sites

Hi,

 

No settings in Gmail >> Settings.

 

I need to know if imap_delete & imap_purse work with GMail.

At another forum, I am advised to move the mail into GMail/Trash folder to delete the emails.

But I have no clue how to do this in PHP.

 

Please help!!

 

Thx

Vai

Link to comment
Share on other sites

Im not sure but ill give this a shot:

 

You are probably getting all the deleted mail (simply in Trash Folder) in the "All Mail" tab, since its... ALL the mail.

 

You are probably going to have to remove the messages from the trash folder;

 

but to be honest, i'm not sure you really need this, personally: i would "move" all the matched mail to the Junk folder, that way the user can simply check through in case any important mail was mistakenly deleted.

 

remember- php.net is extremely useful in these situations, me typing in php.net/imap gave me all the information i would need to do all this myself (having not done it before...ever)

 

to get you started: http://uk2.php.net/manual/en/function.imap-mail-move.php

Check through the list of functions on the left for others that may work, just try the examples, if they dont work try another :P.

Link to comment
Share on other sites

Like so... (Google doesn't use message number(s), only uid(s) for the delete command, as per RFC standard)

 

<?php

$deleted = 0;

$mailbox = imap_open ( "{imap.gmail.com:993/imap/ssl}INBOX", "user","pass" ) or die ( 'Connection Error: ' .  imap_last_error () );

$check = imap_check ( $mailbox ) or die ( 'Check Mail Box Error: ' .  imap_last_error () );

echo "Number of messages : " . $check->Nmsgs . "\r\n<br />\r\n";

$view  = @imap_fetch_overview ( $mailbox, '1:' . $check->Nmsgs, 0 ) or die ( 'Count Messages Error: ' .  imap_last_error () );

$size  = ( sizeof ( $view ) - 1 );

/* loop the box newest to oldest */

for ( $x = $size; $x >= 0; $x-- )
{
	if ( preg_match ( '/night/i', $view[$x]->subject ) > 0 )
	{
		@imap_delete ( $mailbox, $view[$x]->uid, FT_UID );

		@imap_expunge ( $mailbox );

		$deleted++;
	}
}

imap_close ( $mailbox );

echo "Deleted: " . $deleted . " message(s)\r\n<br />\r\n";

echo "DONE";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.