ロリポップのmailサーバー一括削除

普通にロリポップにメールが溜まりすぎて消そうと思ったが多すぎて(1回50件だけ…)
大変。。。警告

あなたのメールボックスはほぼいっぱいです。

47.99 ギガバイト50 ギガバイト


はてどうしたものか?一気に削除したい。以下で解決

<?php
// ===== 設定 =====
$host = 'imap.lolipop.jp';   // ロリポップのIMAPサーバ
$port = 993;                 // SSLの標準IMAPポート
$user = 'info@xxx.com'; // あなたのメールアドレス
$pass = 'xxxxxxx';     // パスワード(公開後は必ず変更!)
$targetFolder = 'INBOX';     // 対象フォルダ(例:INBOX, Spamなど)
$days = 4;                   // ○日より前を対象(当日含まず)
$dryRun = false;               // true=削除せず件数表示、false=削除実行
$batchSize = 1000;           // 一度に処理する件数
$sleepMs = 300;              // バッチ間休止(ミリ秒)

// ===== 共通設定 =====
mb_language("ja");
mb_internal_encoding("UTF-8");
date_default_timezone_set('Asia/Tokyo');
ini_set('display_errors', 0);
error_reporting(E_ALL);
set_time_limit(0);

// ===== 接続 =====
$mailboxStr = sprintf('{%s:%d/imap/ssl/novalidate-cert}%s', $host, $port, $targetFolder);
$mbox = @imap_open($mailboxStr, $user, $pass);
if (!$mbox) {
    exit('IMAP接続失敗: ' . imap_last_error());
}
echo "IMAP接続成功\n";

// ===== 検索 =====
$beforeDate = date('d-M-Y', strtotime("-{$days} days"));
$criteria = 'BEFORE "' . $beforeDate . '"';
$uids = imap_search($mbox, $criteria, SE_UID);

if (!$uids || empty($uids)) {
    echo "該当なし(条件: {$criteria})\n";
    imap_close($mbox);
    exit;
}

echo "該当件数: " . count($uids) . "({$days}日より前, BEFORE {$beforeDate})\n";

if ($dryRun) {
    echo "[DRY RUN] 削除しません\n";
    imap_close($mbox);
    exit;
}

// ===== バッチ削除 =====
$totalDeleted = 0;
$chunks = array_chunk($uids, $batchSize);
foreach ($chunks as $i => $chunk) {
    $deleted = 0;
    foreach ($chunk as $uid) {
        if (imap_delete($mbox, (string)$uid, FT_UID)) {
            $deleted++;
        }
    }
    if ($deleted > 0) {
        if (imap_expunge($mbox)) {
            $totalDeleted += $deleted;
            echo sprintf("Batch %d/%d: 削除 %d件(累計 %d件)\n",
                         $i+1, count($chunks), $deleted, $totalDeleted);
        } else {
            echo "ERROR: expunge失敗: " . imap_last_error() . "\n";
        }
    }
    usleep($sleepMs * 1000); // サーバー負荷軽減
}

imap_close($mbox);
echo "== 完了: 総削除件数 {$totalDeleted} ==\n";

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です