Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete all email from account
    text
    copied!<p>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.</p> <p>The most recent being:</p> <pre><code>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 somebody@gmail.com $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; } </code></pre> <p>Any ideas what I'm missing or is there a cleaner way to do it?</p> <p>To answer the question of why:</p> <p>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</p> <p>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?</p> <pre><code>// DELETE ALL EMAILS IN ACCOUNT </code></pre> <p>function deleteEmails($emailAddress) { $result = "error"; // DOWNLOAD DATA // the max time allows for the email to download set_time_limit(30000);</p> <pre><code> // connect to gmail with your credentials $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; $username = $emailAddress; # e.g somebody@gmail.com $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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload