Skip to content

Instantly share code, notes, and snippets.

@tochiz
Created November 15, 2011 13:33
Show Gist options
  • Save tochiz/1367080 to your computer and use it in GitHub Desktop.
Save tochiz/1367080 to your computer and use it in GitHub Desktop.
delete all mail in inbox from imap mail box.
<?php
/*
* delete_inbox.php
* inboxを空にする。cronなどで回す
*/
// IDとパスワードとホスト名。SSL検証はエラー出るので回避。
$id = '';
$ps = '';
$host = '{imap.ezweb.ne.jp:993/imap/ssl/novalidate-cert}';
// 削除中ならば終了
if(file_exists(dirname(__FILE__).'/.deletenow'))
exit();
// 削除開始
touch(dirname(__FILE__).'/.deletenow');
// 受信BOXを開く
$mbox = imap_open($host.'INBOX', $id, $ps);
// 現在のメール数を取得&0になるまで繰り返す
while($num = imap_num_msg($mbox))
{
echo "現在のメッセージ数: $num\n";
echo "削除中...\n";
// Deleteフラグを全てにセット
imap_setflag_full($mbox, "1:$num", "\\Deleted");
// 実際に削除する
imap_expunge ($mbox);
}
// 接続を閉じる
imap_close($mbox);
// 削除終了
unlink(dirname(__FILE__).'/.deletenow');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment