Installation
composer require daddl3/imap-symfony_bundle
Register the bundle in config/bundles.php:
return [
// ...
Daddl3\ImapSymfonyBundle\Daddl3ImapSymfonyBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console daddl3:imap:install
Edit config/packages/daddl3_imap.yaml with your IMAP server details (host, port, username, password, SSL settings).
First Use Case Fetch unread emails in a controller:
use Daddl3\ImapSymfonyBundle\Service\ImapService;
class EmailController extends AbstractController
{
public function __construct(private ImapService $imapService) {}
public function index()
{
$unreadEmails = $this->imapService->getUnreadEmails();
return $this->render('email/index.html.twig', ['emails' => $unreadEmails]);
}
}
Email Fetching
$emails = $this->imapService->getEmails(['limit' => 10]);
$unread = $this->imapService->getEmails(['unread' => true]);
$flagged = $this->imapService->getEmails(['flagged' => true]);
Email Processing
foreach ($emails as $email) {
$body = $email->getBody();
$attachments = $email->getAttachments();
}
$this->imapService->markAsRead($email->getId());
Searching
$results = $this->imapService->search('SINCE "01-Jan-2023"');
Event Listeners
EmailFetchedEvent):
# config/services.yaml
services:
App\EventListener\EmailListener:
tags:
- { name: 'kernel.event_listener', event: 'daddl3.imap.email.fetched', method: 'onEmailFetched' }
$cache = $this->cache->get('unread_emails_count', function() {
return $this->imapService->countUnreadEmails();
});
{% for email in emails %}
<div class="email">
<h3>{{ email.subject }}</h3>
<p>{{ email.body|truncate(200) }}</p>
</div>
{% endfor %}
Connection Issues
Connection could not be established.daddl3_imap.yaml settings (host, port, SSL, authentication). Test with a standalone IMAP client (e.g., Thunderbird) first.daddl3_imap:
debug: true
Character Encoding
$emails = $this->imapService->getEmails(['charset' => 'UTF-8']);
Rate Limiting
sleep(1); // Pause between API calls
Attachment Handling
Webklex\IMAP\Mailbox::getMessages() with fetchBody: true and fetchAttachments: true. Validate file paths and permissions.daddl3_imap tab for raw IMAP traffic.dovecot) for development to avoid cloud provider delays.default_socket_timeout in php.ini if emails hang:
default_socket_timeout = 300
Custom Mailbox Classes
Extend Webklex\IMAP\Mailbox to add domain-specific logic:
namespace App\IMAP;
use Webklex\IMAP\Mailbox as BaseMailbox;
class CustomMailbox extends BaseMailbox
{
public function getCustomField()
{
return $this->getHeader('X-Custom-Header');
}
}
Register in services.yaml:
services:
App\IMAP\CustomMailbox: '@daddl3_imap.mailbox'
Event Customization Dispatch custom events for business logic:
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class EmailService
{
public function __construct(
private EventDispatcherInterface $dispatcher
) {}
public function processEmail($email)
{
$event = new CustomEmailEvent($email);
$this->dispatcher->dispatch($event);
}
}
Configuration Overrides Override bundle config per environment:
# config/packages/dev/daddl3_imap.yaml
daddl3_imap:
host: 'localhost'
port: 143
ssl: false
How can I help you explore Laravel packages today?