Search code examples
phpgmail

Delete all email from account


I'm trying to put together a function in php that will log into gmail and delete all the emails in the inbox. That's it. I'm at a bit of a standstill with it and have tried a number of ways to do it including reworking other code to try and get it to work but with limited success.

The most recent being:

function deleteEmails($emailAddress, $reportUrl, $reportType)
{
    $result = "error";
    // DOWNLOAD DATA
    // the max time allows for the email to download
    set_time_limit(30000);

    // connect to gmail with your credentials
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = $emailAddress; # e.g [email protected]
    $password = $superSecretPasswordShhhhhhhh;


    // try to connect
    $inbox = imap_open($hostname,$username,$password) or die('Cannot download information: ' . imap_last_error());
    $emails = imap_search($inbox,'ALL');
    // if any emails found, iterate through each email
    if($emails)
        {
            $count = 1;

            // for every email...
            foreach($emails as $email_number) 
                {
                    // TRIED BOTH, BUT THE EMAILS WOULDN'T DELETE
                    //imap_delete($inbox,$email_number);
                    imap_mail_move($inbox, $email_number,'[Gmail]/Bin');
                    $result = "success";
                }
        } 
    // close the connection
    imap_close($inbox,CL_EXPUNGE);
    return $result;
}

Any ideas what I'm missing or is there a cleaner way to do it?

To answer the question of why:

There is an application that loops a function that downloads email from the account and saves the attached report. This works fine but the problem is that the reports arrive every minute and so when the function is run it could have hundreds of reports to go through. So to clean the backlog before starting the process would be the best thing to keep the inbox clean

The following is the code as it stands. It works in that it deletes the emails but even if the emails are all gone it runs until I get a server error. Any ideas what I might be missing?

// DELETE ALL EMAILS IN ACCOUNT

function deleteEmails($emailAddress) { $result = "error"; // DOWNLOAD DATA // the max time allows for the email to download set_time_limit(30000);

    // connect to gmail with your credentials
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = $emailAddress; # e.g [email protected]
    $password = $superSecretPasswordShhhhh;


    // try to connect
    $inbox = imap_open($hostname,$username,$password) or die('Cannot download information: ' . imap_last_error());
    $emails = imap_search($inbox,'ALL');



    // if any emails found, iterate through each email
    if($emails)
        {
            $count = 1;

            // put the newest emails on top
            rsort($emails);


            // for every email...
            foreach($emails as $email_number) 
                {
                    // TESTING BOTH METHODS
                    imap_delete($inbox,$email_number);
                    //imap_mail_move($inbox, $email_number,'[Gmail]/Bin');
                    $result = "success";
                }
        } 
    // close the connection
    imap_expunge($inbox);
    imap_close($inbox,CL_EXPUNGE);
    return $result;
}

Solution

  • You should call this before closing the connection:

    imap_expunge($inbox);
    

    This will delete all messages you marked for removal.

    Replace imap_mail_move with imap_delete first though.

    Read more about it: http://php.net/imap-expunge