barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.65k stars 459 forks source link

#529: Add email flags as properties #653

Closed Sebbo94BY closed 2 years ago

Sebbo94BY commented 2 years ago

This change adds all available email flags as property:

Those can be accessed like this:

<?php
    require_once __DIR__.'/../vendor/autoload.php';

    use PhpImap\Exceptions\ConnectionException;
    use PhpImap\Mailbox;

    $mailbox = new Mailbox(
        '{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
        'some@gmail.com', // Username for the before configured mailbox
        '*********', // Password for the before configured username
        __DIR__, // Directory, where attachments will be saved (optional)
        'US-ASCII' // Server encoding (optional)
    );

    try {
        $mail_ids = $mailbox->searchMailbox('ALL');
    } catch (ConnectionException $ex) {
        exit('IMAP connection failed: '.$ex->getErrors('first'));
    } catch (Exception $ex) {
        exit('An error occured: '.$ex->getMessage());
    }

    foreach ($mail_ids as $mail_id) {
        echo "+------ P A R S I N G ------+\n";

        $email = $mailbox->getMail(
            $mail_id, // ID of the email, you want to get
            false // Do NOT mark emails as seen (optional)
        );

        echo 'mail has been seen? ';
        if ($email->isSeen) {
            echo "Yes\n";
        } else {
            echo "No\n";
        }

        echo 'mail has been answered? ';
        if ($email->isAnswered) {
            echo "Yes\n";
        } else {
            echo "No\n";
        }

        echo 'mail is a draft? ';
        if ($email->isDraft) {
            echo "Yes\n";
        } else {
            echo "No\n";
        }
    }

    $mailbox->disconnect();

Solves #529.