adachsoft / mailbox-contracts
Provider-independent PHP contracts and immutable models for searching, reading, synchronizing, and sending e-mail.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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, andSyncTokenare values to pass back to the provider implementation. Consumers must not parse them or reuse them with another provider. AMessageIdis guaranteed only for the message's current location: aftermoveToFolder()ortrash()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:
MailboxCapabilitySetadvertises optional operations. Implementations reject unsupported operations withUnsupportedOperationExceptionrather than silently approximating them. - Folders and labels: folders represent mailbox locations; labels are an independent classification mechanism.
SpecialFolderEnumprovides portable names for common folders, while provider-specific folders and labels use opaque IDs. - Cursors:
PageCursoris opaque. Pass the returnednextCursorto the nextsearch()call; do not construct or inspect cursors in application code. - Synchronization:
SyncTokenis opaque.changesSince()returns added, removed, and flag-changed message IDs, together with the next token. An expired or unknown token is reported throughfullResyncRequired.flagsChangedis filled only by providers advertisingFLAG_CHANGE_TRACKING(IMAP needs the CONDSTORE extension); withCHANGE_TRACKINGalone it is always empty.
Usage
Interfaces
MailboxReaderInterfacediscovers capabilities, searches summaries, retrieves complete messages and attachment content, and tracks mailbox changes.MailboxManagerInterfacechanges seen state, moves or trashes messages, and manages labels.MailSenderInterfacesends anOutgoingMessageDtoand returns aSentMessageDto.MailboxInterfacecombines 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 concept | IMAP/SMTP mapping | Gmail API mapping |
|---|---|---|
| Message ID | Opaque value containing mailbox/folder identity, UIDVALIDITY, and UID | Gmail message id |
| Folders and labels | IMAP folders; provider labels may be unavailable | Gmail labels, including system labels that represent special folders |
| Search cursor | Opaque cursor over an IMAP UID range or result offset | Opaque cursor wrapping Gmail pageToken |
| Sync token | Opaque value based on UIDNEXT and, when available, MODSEQ | Opaque value based on Gmail historyId |
| Send | SMTP; optionally append the sent copy to the Sent folder | Gmail 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.