digitalshift/mailbox-persistence-bundle
Installation Add the bundle via Composer:
composer require digitalshift/mailbox-persistence-bundle
Enable it in config/bundles.php:
return [
// ...
Digitalshift\MailboxPersistenceBundle\DigitalshiftMailboxPersistenceBundle::class => ['all' => true],
];
Configuration
Update config/packages/digitalshift_mailbox_persistence.yaml (or create it):
digitalshift_mailbox_persistence:
driver: 'doctrine' # or 'filesystem' for testing
filesystem:
directory: '%kernel.project_dir%/var/mailbox'
doctrine:
entity: App\Entity\MailboxMessage # Your custom entity
repository: App\Repository\MailboxMessageRepository
First Use Case Persist an incoming email (e.g., in a Symfony Mailer transport listener):
use Digitalshift\MailboxPersistenceBundle\Persistence\MailboxPersisterInterface;
public function handle(Message $message, Transport $transport)
{
$persister = $this->container->get(MailboxPersisterInterface::class);
$persister->persist($message);
}
Email Persistence
MailboxPersisterInterface in a Symfony TransportListener or MessageListener.SwiftMailer event listeners (e.g., SentEvent).$persister->persist($message, [
'user_id' => $user->getId(), // Custom metadata
'folder' => 'sent',
]);
Attachment Handling
filesystem or linked via doctrine (configurable).getAttachments() method or repository queries.Rendering/Editing
MailboxRenderer service to generate HTML/Plaintext previews:$renderer = $this->container->get('digitalshift_mailbox_persistence.renderer');
$html = $renderer->render($mailboxMessage);
Swift Forwarding
MailboxForwarder to forward persisted emails via SwiftMailer:$forwarder = $this->container->get('digitalshift_mailbox_persistence.forwarder');
$forwarder->forward($mailboxMessage, $recipientEmail);
MailboxMessage entity to add custom fields (e.g., readAt, labels).PersistMailboxMessage messages for async processing.#[Route('/mailbox/{id}', methods: ['GET'])]
public function getMailboxMessage(MailboxMessage $message): JsonResponse
{
return $this->json($message->toArray());
}
Filesystem Permissions
var/mailbox is writable by the web server (e.g., chmod -R 775 var/mailbox).Doctrine Entity Mapping
doctrine, ensure your custom entity maps to the expected fields (e.g., from, to, subject, body).MailboxMessage entity by configuring doctrine.entity in the bundle config.Attachment Storage
var/. For production, use cloud storage (e.g., AWS S3) via a custom AttachmentStorage service.SwiftMailer Events
# config/packages/monolog.yaml
handlers:
mailbox:
type: stream
path: "%kernel.logs_dir%/mailbox.log"
level: debug
channels: ["digitalshift_mailbox"]
php bin/console digitalshift:mailbox:list
Custom Storage
Override AttachmentStorageInterface for S3/DB storage:
services:
App\Storage\S3AttachmentStorage:
arguments:
- '@aws_s3.client'
tags: ['digitalshift_mailbox.attachment_storage']
Event Listeners
Extend persistence logic via events (e.g., MailboxMessagePersistedEvent):
#[AsEventListener(event: MailboxMessagePersistedEvent::class)]
public function onPersisted(MailboxMessagePersistedEvent $event) {
// Add custom logic (e.g., index in Elasticsearch)
}
Renderer Decorators
Decorate MailboxRenderer to modify output (e.g., add tracking pixels):
services:
digitalshift_mailbox_persistence.renderer:
decorates: digitalshift_mailbox_persistence.renderer
arguments: ['@digitalshift_mailbox_persistence.renderer.inner']
How can I help you explore Laravel packages today?