digitalshift/mailbox-connection-bundle
Installation
composer require digitalshift/mailbox-connection-bundle
Ensure pecl/mailparse is installed (pecl install mailparse).
Enable the Bundle
Add to config/bundles.php:
return [
// ...
Digitalshift\MailboxConnectionBundle\DigitalshiftMailboxConnectionBundle::class => ['all' => true],
];
Configure
Define credentials in config/packages/digitalshift_mailbox_connection.yaml:
digitalshift_mailbox_connection:
connections:
default:
protocol: imap
host: 'imap.example.com'
port: 993
username: 'user@example.com'
password: 'password'
ssl: true
First Use Case Fetch the inbox folder and list messages in a controller:
use Digitalshift\MailboxConnectionBundle\Entity\Folder;
use Digitalshift\MailboxConnectionBundle\Entity\MimeMessage;
public function inboxAction(ImapConnector $connector): Response
{
$folder = $connector->getFolder('INBOX');
$messages = $folder->getMessages();
return $this->render('mailbox/inbox.html.twig', [
'messages' => $messages,
]);
}
Folder Management
$rootFolder = $connector->getFolder('');
$subfolders = $rootFolder->getSubFolders();
$folder = $connector->getFolder('INBOX/Subfolder');
Message Processing
$messages = $folder->getMessages(0, 10); // offset, limit
/** @var MimeMessage $message */
$subject = $message->getSubject();
$from = $message->getFrom();
$date = $message->getDate();
$parts = $message->getParts();
foreach ($parts as $part) {
if ($part->isAttachment()) {
$attachment = $part->getContent();
}
}
Searching Messages
$searchCriteria = [
'SINCE' => '01-Jan-2023',
'FROM' => 'sender@example.com',
'SUBJECT' => 'important',
];
$messages = $folder->searchMessages($searchCriteria);
Dependency Injection
ImapConnector into services/controllers:
public function __construct(private ImapConnector $connector) {}
digitalshift.mailbox.folder.loaded).$cache = $this->get('cache.app');
$cacheKey = 'mailbox_folders_' . md5($folderPath);
$folders = $cache->get($cacheKey, function() use ($connector, $folderPath) {
return $connector->getFolder($folderPath)->getSubFolders();
});
PECL Dependency
pecl/mailparse may not be pre-installed on shared hosting.pecl install mailparse or use a Docker container with the dependency pre-configured.SSL/TLS Configuration
ssl_verify_peer).ImapConnector to pass additional stream context options:
$connector->setStreamContext([
'ssl' => [
'verify_peer' => false,
'allow_self_signed' => true,
],
]);
Memory Limits
setMemoryLimit() on the connector.Folder Path Case Sensitivity
$folderPath = strtoupper('INBOX/subfolder');
Connection Timeouts
digitalshift_mailbox_connection:
connections:
default:
timeout: 30
Enable Verbose Logging
Add to config/packages/monolog.yaml:
handlers:
mailbox:
type: stream
path: "%kernel.logs_dir%/mailbox.log"
level: debug
Then enable debug mode in the connector:
$connector->setDebug(true);
Check IMAP Errors Wrap connector calls in try-catch:
try {
$folder = $connector->getFolder('INBOX');
} catch (\Digitalshift\MailboxConnectionBundle\Exception\ImapException $e) {
// Log or handle error (e.g., retry logic)
}
Custom MIME Parsing
Extend MimeMessage or override the mailparse behavior by creating a custom MimeMessageFactory:
class CustomMimeMessageFactory extends MimeMessageFactory
{
public function createFromRaw($rawMessage): MimeMessage
{
// Custom logic here
return parent::createFromRaw($rawMessage);
}
}
Register it as a service:
services:
Digitalshift\MailboxConnectionBundle\Factory\MimeMessageFactory:
class: App\Factory\CustomMimeMessageFactory
Protocol-Specific Logic
Use the ConnectorInterface to create protocol-agnostic services:
public function processMessages(ConnectorInterface $connector, string $folderPath): void
{
$folder = $connector->getFolder($folderPath);
// Works for IMAP/POP3 (when implemented)
}
Custom Folder Entities
Extend the Folder entity to add metadata:
class CustomFolder extends Folder
{
private $customMetadata;
public function setCustomMetadata($metadata): self
{
$this->customMetadata = $metadata;
return $this;
}
}
Override the factory to return your custom entity.
Async Processing
Use Symfony’s Messenger to process messages in the background:
$messageHandler = new ProcessMailMessageHandler($connector);
$this->messageBus->dispatch(new ProcessMailMessage($message));
How can I help you explore Laravel packages today?