How can I check if there are new emails in the INBOX using PHP and imap_check or imap_status functions?

To check if there are new emails in the INBOX using PHP, you can use the imap_check or imap_status functions provided by the IMAP extension. These functions allow you to retrieve information about the current status of the mailbox, including the number of recent messages.

<?php

$hostname = '{imap.example.com:993/imap/ssl}INBOX';
$username = 'your_email@example.com';
$password = 'your_password';

$mailbox = imap_open($hostname, $username, $password);

$mailbox_check = imap_check($mailbox);

if ($mailbox_check) {
    $num_new_emails = $mailbox_check->Recent;

    if ($num_new_emails > 0) {
        echo "You have $num_new_emails new email(s) in your INBOX.";
    } else {
        echo "No new emails in your INBOX.";
    }
}

imap_close($mailbox);

?>