我正在尝试在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
function 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;
}
答案 0 :(得分:1)
你应该在关闭连接之前调用它:
imap_expunge($inbox);
这将删除您标记为要删除的所有邮件。
首先将imap_mail_move
替换为imap_delete
。