Installation:
composer require secit-pl/imap-bundle
(Note: The README mentions secit-pl/imap-bundle, but the package name in the description is ehymel/imap-bundle. Verify the correct package name in your project.)
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
Secit\ImapBundle\SecitImapBundle::class => ['all' => true],
];
Configure IMAP Connection:
Define connection settings in config/packages/secit_imap.yaml:
secit_imap:
connections:
default:
host: 'imap.example.com'
port: 993
encryption: ssl
username: 'your_username'
password: 'your_password'
validate_cert: true
First Use Case: Fetch emails in a controller:
use Secit\ImapBundle\Service\ImapService;
class EmailController extends AbstractController
{
public function __construct(private ImapService $imapService) {}
public function index()
{
$emails = $this->imapService->getEmails('default');
return $this->render('emails/index.html.twig', ['emails' => $emails]);
}
}
Connection Management:
default, work, personal) for multi-account setups.$this->imapService->setConnection('work');
Email Retrieval:
$emails = $this->imapService->getEmails('default');
$emails = $this->imapService->getEmails('default', ['unread' => true]);
$emails = $this->imapService->getEmails('default', ['since' => '2023-01-01']);
Email Processing:
foreach ($emails as $email) {
$subject = $email->getSubject();
$from = $email->getFrom();
$body = $email->getBody();
}
foreach ($email->getAttachments() as $attachment) {
$attachment->saveTo('/path/to/save');
}
Searching Emails:
BODY "invoice", FROM "support@example.com"):
$emails = $this->imapService->searchEmails('default', 'BODY "receipt"');
Sending Emails (if supported):
secit_imap.yaml and use:
$this->imapService->sendEmail([
'to' => 'recipient@example.com',
'subject' => 'Hello',
'body' => 'Email body',
]);
Symfony Events:
post.imap.email.fetched):
# config/services.yaml
Secit\ImapBundle\EventListener\ImapListener:
tags:
- { name: kernel.event_listener, event: post.imap.email.fetched, method: onEmailFetched }
Caching:
$emails = $this->cache->get('emails_list', function() {
return $this->imapService->getEmails('default');
});
Background Processing:
$this->messageBus->dispatch(new ProcessEmailMessage($email));
Testing:
ImapService in tests:
$this->mockImapService->expects($this->once())
->method('getEmails')
->willReturn([$mockEmail]);
Connection Issues:
validate_cert: false (not recommended for production) or ensure your server has a valid certificate.secit_imap.yaml:
debug: true
Check logs for IMAP errors.Performance:
limit parameter:
$this->imapService->getEmails('default', ['limit' => 50]);
Or fetch only headers first:
$this->imapService->getEmailHeaders('default');
Encoding Issues:
charset is set in secit_imap.yaml:
secit_imap:
default_charset: 'UTF-8'
Attachment Handling:
$attachment->saveTo(sys_get_temp_dir() . '/attachment.pdf');
Deprecation:
php-imap (direct library).spatie/laravel-mail (for sending).symfony/mime (for parsing).Enable Verbose Logging:
secit_imap:
debug: true
log_level: debug
Raw IMAP Commands:
php-imap object for advanced debugging:
$imapStream = $this->imapService->getImapStream('default');
$rawResponse = imap_mailboxmsginfo($imapStream);
Common IMAP Errors:
[PHP_IMAP] IMAP Error: Check credentials, server status, and firewall rules.Connection timed out: Increase timeout in config:
secit_imap:
connections:
default:
timeout: 30
Custom Email Classes:
Secit\ImapBundle\Entity\Email to add custom methods:
class CustomEmail extends Email
{
public function isUrgent(): bool
{
return str_contains($this->getSubject(), 'URGENT');
}
}
# config/services.yaml
Secit\ImapBundle\Service\ImapService:
arguments:
$emailClass: App\Entity\CustomEmail
Custom Search Criteria:
ImapService to wrap complex searches:
public function getUnreadInvoices(string $connectionName): array
{
return $this->searchEmails($connectionName, 'UNSEEN BODY "invoice"');
}
Event Dispatching:
// src/EventListener/EmailListener.php
public function onEmailFetched(EmailFetchedEvent $event)
{
if ($event->getEmail()->isUrgent()) {
$this->notificationService->alert('Urgent email received!');
}
}
Connection Pooling:
$this->imapService->persistConnection('default');
How can I help you explore Laravel packages today?