adachsoft / mailbox-imap
IMAP mailbox adapter with SMTP message sending for the AdachSoft mailbox contracts.
Requires
- php: ^8.3
- adachsoft/mailbox-contracts: ^0.2
- directorytree/imapengine: ^1.26
- symfony/mailer: ^7.3 || ^8.0
Requires (Dev)
- adachsoft/mailbox-contract-tests: ^0.2
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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
| Capability | Support |
|---|---|
| Server-side search | Supported |
| Read messages and attachments | Supported |
| Seen/unseen flags | Supported |
| Move messages and trash | Supported |
| Change tracking | Supported: UIDNEXT tracks additions; CONDSTORE tracks flag changes when advertised by the server |
| Send messages | Supported through SMTP; saving a copy to Sent is optional |
| Labels | Not supported by IMAP folders |
| Threads | Not supported |
| QRESYNC removals | Not 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
MessageIdis 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.PageCursoris 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.SyncTokenrepresents UIDVALIDITY, UIDNEXT, and, when available, the highest CONDSTORE modification sequence. Treat it as opaque and pass the returnednextSyncTokento the nextchangesSince()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.