Search by

adachsoft / mailbox-imap

Arkadiusz Adach

IMAP mailbox adapter with SMTP message sending for the AdachSoft mailbox contracts.

Package info

gitlab.com/a.adach/mailbox-imap

Issues

pkg:composer/adachsoft/mailbox-imap

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-25 10:58 UTC

This package is auto-updated.

Last update: 2026-11-25 10:00:33 UTC


README

adachsoft/mailbox-imap implements the provider-independent mailbox contract over IMAP for reading and management, with SMTP message sending through Symfony Mailer. It uses DirectoryTree ImapEngine for IMAP transport operations.

Requirements

  • PHP 8.3 or newer.

Installation

composer require adachsoft/mailbox-imap

The package installs the required mailbox contracts and Symfony Mailer dependencies.

Usage

<?php

declare(strict_types=1);

use AdachSoft\MailboxContracts\Model\MailAddress;
use AdachSoft\MailboxContracts\Model\MessageQuery;
use AdachSoft\MailboxContracts\Model\OutgoingMessageDto;
use AdachSoft\MailboxImap\Config\ImapConnectionConfig;
use AdachSoft\MailboxImap\Config\ImapEncryptionEnum;
use AdachSoft\MailboxImap\Config\SmtpConnectionConfig;
use AdachSoft\MailboxImap\Config\SmtpEncryptionEnum;
use AdachSoft\MailboxImap\ImapSmtpMailbox;

$imap = new ImapConnectionConfig(
    host: 'imap.example.test',
    port: 993,
    encryption: ImapEncryptionEnum::SSL,
    username: 'mailbox-user',
    password: 'imap-password',
);

$smtp = new SmtpConnectionConfig(
    host: 'smtp.example.test',
    port: 465,
    encryption: SmtpEncryptionEnum::SSL,
    username: 'mailbox-user',
    password: 'smtp-password',
    defaultFrom: new MailAddress('mailbox@example.test', 'Mailbox'),
);

$mailbox = ImapSmtpMailbox::create($imap, $smtp);

$messages = $mailbox->search(new MessageQuery(unreadOnly: true));
$sent = $mailbox->send(OutgoingMessageDto::simple(
    new MailAddress('recipient@example.test'),
    'Hello',
    'Sent through SMTP.',
));

Use SmtpEncryptionEnum::STARTTLS for an SMTP connection upgraded with STARTTLS, or SmtpEncryptionEnum::NONE for an unencrypted connection. The message From address takes precedence over defaultFrom; sending fails with MailTransportException if neither is set. SMTP credentials are optional.

ImapSmtpMailbox::create() enables saving sent messages to the IMAP Sent folder by default. Pass false as its third argument to disable that behavior:

$mailbox = ImapSmtpMailbox::create($imap, $smtp, appendSentCopy: false);

When enabled, the message is appended only if the Sent folder can be resolved and is available. The package resolves it using IMAP SPECIAL-USE attributes or configured overrides. A successful SMTP send is still returned when no Sent folder is available.

Capabilities

CapabilitySupport
Server-side searchSupported
Read messages and attachmentsSupported
Seen/unseen flagsSupported
Move messages and trashSupported
Change trackingSupported: UIDNEXT tracks additions; CONDSTORE tracks flag changes when advertised by the server
Send messagesSupported through SMTP; saving a copy to Sent is optional
LabelsNot supported by IMAP folders
ThreadsNot supported
QRESYNC removalsNot supported; removed messages are not reported by change tracking

Special folders

INBOX always resolves to the reserved IMAP INBOX path. Other special folders are resolved from server-advertised SPECIAL-USE attributes. Entries in ImapConnectionConfig::$specialFolderOverrides take precedence and use the special-folder enum value as the key:

use AdachSoft\MailboxContracts\Model\SpecialFolderEnum;

$imap = new ImapConnectionConfig(
    host: 'imap.example.test',
    port: 993,
    encryption: ImapEncryptionEnum::SSL,
    username: 'mailbox-user',
    password: 'imap-password',
    specialFolderOverrides: [
        SpecialFolderEnum::SENT->value => 'Sent Items',
        SpecialFolderEnum::TRASH->value => 'Deleted Items',
    ],
);

Identifiers and synchronization

  • MessageId is opaque to callers. Its current IMAP representation is an unpadded base64url encoding of the folder path, UIDVALIDITY, and UID. A UID is valid only for its folder and UIDVALIDITY value.
  • PageCursor is also opaque. Its current representation is unpadded base64url-encoded UIDVALIDITY and UID paging state. A cursor becomes invalid when the folder's UIDVALIDITY changes; restart the search in that case.
  • SyncToken represents UIDVALIDITY, UIDNEXT, and, when available, the highest CONDSTORE modification sequence. Treat it as opaque and pass the returned nextSyncToken to the next changesSince() call. A changed UIDVALIDITY or invalid token requires a full resynchronization. Deletions are not reported until QRESYNC support is added.

Tests

The default PHPUnit run executes unit tests only:

vendor/bin/phpunit

The separate integration contract suite can be run against GreenMail. Start the server with the same ports and test account used by the suite:

docker run --rm -e GREENMAIL_OPTS='-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.users=test:test@example.test' -p 3143:3143 -p 3025:3025 greenmail/standalone:2.1.2

Configure the connection variables and run the integration suite explicitly:

export MAILBOX_IMAP_TEST_HOST=127.0.0.1
export MAILBOX_IMAP_TEST_IMAP_PORT=3143
export MAILBOX_IMAP_TEST_SMTP_PORT=3025
export MAILBOX_IMAP_TEST_USER=test
export MAILBOX_IMAP_TEST_PASSWORD=test
vendor/bin/phpunit --testsuite integration

The integration contract test purges the configured account's INBOX before each test. Use a disposable test mailbox.