vaibhavs Posted February 3, 2009 Share Posted February 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted February 3, 2009 Share Posted February 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
vaibhavs Posted February 4, 2009 Author Share Posted February 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 4, 2009 Share Posted February 4, 2009 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 . Quote Link to comment Share on other sites More sharing options...
printf Posted February 4, 2009 Share Posted February 4, 2009 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"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.