我正在尝试在 php 中组合一个函数,该函数将登录 gmail 并删除收件箱中的所有电子邮件。就是这样。我对此有点停顿,并尝试了多种方法来做到这一点,包括重新编写其他代码以尝试使其正常工作,但收效甚微。
最近的是:
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;
}
任何想法我错过了什么或者有更清洁的方法吗?
要回答为什么的问题:
有一个应用程序可以循环从帐户下载电子邮件并保存附加报告的功能。这很好用,但问题是报告每分钟都会到达,所以当函数运行时,它可能有数百个报告要处理。因此,在开始流程之前清理积压是保持收件箱清洁的最佳方式
以下是目前的代码。它的工作原理是删除电子邮件,但即使电子邮件全部消失,它也会运行,直到我收到服务器错误。有什么想法我可能会错过吗?
// DELETE ALL EMAILS IN ACCOUNT
函数 deleteEmails($emailAddress) { $result = "error"; // 下载数据 // 允许电子邮件下载的最长时间 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 = $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;
}