Sử dụng cron job và script IMAP/CLI Tự Động Xoá Mail Cpanel

Bạn có thể dùng cron job để chạy định kỳ mỗi 15 phút và dùng một script xoá email trong inbox.

Bước thực hiện:

  1. Tạo script PHP hoặc shell kết nối tới hộp thư qua IMAP hoặc sử dụng doveadm/mail CLI nếu server hỗ trợ.

  2. Lọc và xóa email trong inbox được nhận trước 15 phút.

  3. Tạo cron job trong cPanel để chạy script đó mỗi 15 phút.

Ví dụ: PHP script đơn giản xóa email cũ hơn 15 phút

<?php
$hostname = '{localhost:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'yourpassword';

$inbox = imap_open($hostname, $username, $password) or die('Cannot connect');
$emails = imap_search($inbox, 'ALL');

if ($emails) {
    foreach ($emails as $email_number) {
        $header = imap_headerinfo($inbox, $email_number);
        $timestamp = strtotime($header->date);
        if (time() - $timestamp > 900) { // 900 giây = 15 phút
            imap_delete($inbox, $email_number);
        }
    }
    imap_expunge($inbox);
}
imap_close($inbox);
?>