secit-pl/imap-bundle
Laravel bundle for IMAP email handling with a simple, configurable client. Connect to mailboxes, search and fetch messages, read headers and bodies, manage folders, and integrate IMAP operations into your Laravel app with minimal setup.
Installation
composer require secit-pl/imap-bundle
Add to config/bundles.php:
Secit\ImapBundle\SecitImapBundle::class => ['all' => true],
Configuration
Define IMAP servers in config/packages/secit_imap.yaml:
servers:
gmail:
host: 'imap.gmail.com'
port: 993
encryption: ssl
username: '%env(IMAP_USERNAME)%'
password: '%env(IMAP_PASSWORD)%'
First Use Case Fetch unread emails from Gmail in a controller:
use Secit\ImapBundle\Service\ImapService;
class EmailController extends AbstractController {
public function __construct(private ImapService $imap) {}
public function listUnread() {
$emails = $this->imap->getEmails('gmail', ['UNSEEN']);
return $this->json($emails);
}
}
Fetching Emails
// Get all emails with specific flags
$this->imap->getEmails('gmail', ['SEEN', 'FLAGGED']);
// Get emails with search criteria
$this->imap->search('gmail', 'SINCE 1-Jan-2023');
Email Processing
foreach ($emails as $email) {
$email->getHeaders(); // Array of headers
$email->getBody(); // Raw body content
$email->getAttachments(); // Array of attachments
}
Attachment Handling
foreach ($email->getAttachments() as $attachment) {
$attachment->saveTo($path); // Save to filesystem
$attachment->getContent(); // Get raw content
}
Email Actions
$this->imap->markAsRead('gmail', $email->getId());
$this->imap->deleteEmail('gmail', $email->getId());
$this->imap->moveEmail('gmail', $email->getId(), 'INBOX/Archive');
ImapService into services/controllers where email logic is needed.$this->imap->afterFetch(function ($emails) {
foreach ($emails as $email) {
$this->dispatch(new EmailFetchedEvent($email));
}
});
$cache = $this->cache->get('gmail_unread_count', function () {
return $this->imap->getEmailCount('gmail', ['UNSEEN']);
});
Connection Issues
Connection could not be established.host, port, and encryption in config. For Gmail, ensure "Less secure apps" is enabled or use an App Password.secit_imap.yaml:
debug: true
Character Encoding
$this->imap->getEmails('gmail', [], ['encoding' => 'UTF-8']);
Rate Limiting
use Symfony\Component\Retry\Retry;
Retry::until(function () use ($imap) {
return $imap->getEmails('gmail');
})->attempts(3)->delay(1000)->execute();
Memory Limits
Allowed memory exhausted when fetching large mailboxes.limit parameter or fetch in batches:
$this->imap->getEmails('gmail', [], ['limit' => 100]);
getEmail() to verify existence before operations.Custom Email Models
Override the default Email class by binding your own:
# config/packages/secit_imap.yaml
email_model: App\Entity\CustomEmail
Pre/Post Hooks
Extend the ImapService to add custom logic:
$this->imap->onFetch(function ($emails) {
// Pre-process emails
});
$this->imap->onDelete(function ($emailId) {
// Post-delete logic
});
Custom Search Criteria Extend the search functionality by adding your own criteria:
$this->imap->extendSearchCriteria(function ($criteria) {
$criteria['CUSTOM'] = 'your_custom_logic';
});
How can I help you explore Laravel packages today?