Search by

adachsoft / mailbox-contracts

Arkadiusz Adach

Provider-independent PHP contracts and immutable models for searching, reading, synchronizing, and sending e-mail.

Package info

gitlab.com/a.adach/mailbox-contracts

Issues

pkg:composer/adachsoft/mailbox-contracts

Statistics

Installs: 20

Dependents: 1

Suggesters: 0

Stars: 0

v0.2.0 2026-11-25 09:10 UTC

This package is auto-updated.

Last update: 2026-11-25 08:11:15 UTC


README

adachsoft/mailbox-contracts provides provider-independent PHP contracts and immutable models for searching, reading, changing, synchronizing, and sending e-mail messages. Implementations adapt these contracts to a mailbox provider while keeping provider-specific identifiers and APIs out of consuming applications.

Design

  • Opaque identifiers: MessageId, AttachmentId, FolderId, LabelId, PageCursor, and SyncToken are values to pass back to the provider implementation. Consumers must not parse them or reuse them with another provider. A MessageId is guaranteed only for the message's current location: after moveToFolder() or trash() the old ID may stop working (IMAP IDs contain the folder and UID; Gmail IDs stay stable), so search the target folder to find the moved message.
  • Capabilities: MailboxCapabilitySet advertises optional operations. Implementations reject unsupported operations with UnsupportedOperationException rather than silently approximating them.
  • Folders and labels: folders represent mailbox locations; labels are an independent classification mechanism. SpecialFolderEnum provides portable names for common folders, while provider-specific folders and labels use opaque IDs.
  • Cursors: PageCursor is opaque. Pass the returned nextCursor to the next search() call; do not construct or inspect cursors in application code.
  • Synchronization: SyncToken is opaque. changesSince() returns added, removed, and flag-changed message IDs, together with the next token. An expired or unknown token is reported through fullResyncRequired. flagsChanged is filled only by providers advertising FLAG_CHANGE_TRACKING (IMAP needs the CONDSTORE extension); with CHANGE_TRACKING alone it is always empty.

Usage

Interfaces

  • MailboxReaderInterface discovers capabilities, searches summaries, retrieves complete messages and attachment content, and tracks mailbox changes.
  • MailboxManagerInterface changes seen state, moves or trashes messages, and manages labels.
  • MailSenderInterface sends an OutgoingMessageDto and returns a SentMessageDto.
  • MailboxInterface combines all three service interfaces for providers that implement every operation.

A reader can be used to process new messages since a previously saved synchronization token:

use AdachSoft\MailboxContracts\Service\MailboxReaderInterface;
use AdachSoft\MailboxContracts\Model\SyncToken;

/** @var MailboxReaderInterface $mailbox */
$changes = $mailbox->changesSince(new SyncToken($savedToken));

if ($changes->fullResyncRequired) {
    // Rebuild local state using search() and get().
} else {
    foreach ($changes->added as $messageId) {
        $message = $mailbox->get($messageId);
        // Process the new message.
    }
}

$nextSavedToken = $changes->nextSyncToken->toString();

For an initial load or a full resynchronization, use search() to retrieve summaries in pages:

use AdachSoft\MailboxContracts\Model\MessageQuery;
use AdachSoft\MailboxContracts\Model\SpecialFolderEnum;

$page = $mailbox->search(new MessageQuery(
    specialFolder: SpecialFolderEnum::INBOX,
    unreadOnly: true,
));

Read a message by its opaque identifier:

$message = $mailbox->get($messageId);

foreach ($message->attachments as $attachmentInfo) {
    $attachment = $mailbox->getAttachment($messageId, $attachmentInfo->id);
    // $attachment->content contains the raw bytes.
}

Send a plain-text message:

use AdachSoft\MailboxContracts\Model\MailAddress;
use AdachSoft\MailboxContracts\Model\OutgoingMessageDto;
use AdachSoft\MailboxContracts\Service\MailSenderInterface;

/** @var MailSenderInterface $sender */
$sent = $sender->send(OutgoingMessageDto::simple(
    new MailAddress('recipient@example.com'),
    'Subject',
    'Message body',
));

Provider mapping

Contract conceptIMAP/SMTP mappingGmail API mapping
Message IDOpaque value containing mailbox/folder identity, UIDVALIDITY, and UIDGmail message id
Folders and labelsIMAP folders; provider labels may be unavailableGmail labels, including system labels that represent special folders
Search cursorOpaque cursor over an IMAP UID range or result offsetOpaque cursor wrapping Gmail pageToken
Sync tokenOpaque value based on UIDNEXT and, when available, MODSEQOpaque value based on Gmail historyId
SendSMTP; optionally append the sent copy to the Sent folderGmail messages.send

Requirements

The package requires PHP 8.3 or newer and depends on adachsoft/collection and psr/clock.

Installation

composer require adachsoft/mailbox-contracts

In-memory implementation for tests

InMemoryMailbox is a deterministic reference implementation for tests of consumers. It supports all capabilities by default, accepts an optional PSR clock for sent-message timestamps, and provides deliver() to seed incoming messages. Its generated message IDs, RFC Message-IDs, page cursors, and synchronization tokens are local to the in-memory instance; do not persist them as provider IDs.

Mailbox implementations are distributed as separate packages. The planned adapters are adachsoft/mailbox-imap and adachsoft/mailbox-gmail.