directorytree/imapengine
IMAPEngine is a Laravel-friendly PHP package for working with IMAP mailboxes. Connect to servers, list folders, fetch messages and attachments, search and manage mail, and handle common IMAP operations through a clean, high-level API.
composer require directorytree/imapengine
use DirectoryTree\ImapEngine\Connection;
use DirectoryTree\ImapEngine\Folder;
$connection = new Connection('imap.example.com', [
'ssl' => true,
'username' => 'user@example.com',
'password' => 'password',
]);
$inbox = $connection->getFolder('INBOX');
$messages = $inbox->query()
->whereUnread()
->withLazyHeaders()
->get();
foreach ($messages as $message) {
echo $message->subject . "\n";
}
HasParsedMessage trait for Eloquent model hydration.$messages = $folder->query()
->withLazyHeaders() // Load headers only when accessed
->withLazyAttachments() // Load attachments only when needed
->get();
$messages = $folder->fetchByUidRange(1000, 2000);
$messages = $folder->query()
->sortByDate('desc')
->get();
$messages = $folder->query()
->whereFrom('sender@example.com')
->whereLargerThan(1024 * 1024) // >1MB
->whereSentSince(now()->subDays(7))
->get();
$folder->bulkQuery()
->whereUnread()
->flag(\DirectoryTree\ImapEngine\Flag::Seen)
->execute();
$folder->poll(function (Folder $folder) {
$newMessages = $folder->query()->whereUnseen()->get();
// Process new messages
}, 30); // Check every 30 seconds
$folder->idle(function (Folder $folder) {
// Handle new messages in real-time
});
foreach ($message->attachments() as $attachment) {
$attachment->saveTo(storage_path('app/attachments/' . $attachment->name));
}
if ($attachment->contentDisposition === 'attachment') {
// Handle as downloadable file
}
// app/Providers/ImapServiceProvider.php
public function register()
{
$this->app->singleton(Connection::class, function ($app) {
return new Connection(config('imap.host'), [
'ssl' => config('imap.ssl'),
'username' => config('imap.username'),
'password' => config('imap.password'),
]);
});
}
// app/Models/Email.php
use DirectoryTree\ImapEngine\HasParsedMessage;
class Email extends Model
{
use HasParsedMessage;
protected $fillable = ['subject', 'body', 'from', 'to', 'uid', 'folder'];
public static function fromImapMessage(Message $message, Folder $folder)
{
return static::create([
'subject' => $message->subject,
'body' => $message->text,
'from' => $message->from->address,
'to' => $message->to->address,
'uid' => $message->uid,
'folder' => $folder->name,
]);
}
}
// app/Console/Commands/SyncEmails.php
public function handle()
{
$connection = app(Connection::class);
$folder = $connection->getFolder('INBOX');
$messages = $folder->query()
->whereUnread()
->get();
foreach ($messages as $message) {
Email::fromImapMessage($message, $folder);
}
}
// app/Listeners/ProcessNewEmail.php
public function handle($event)
{
$message = $event->message;
// Process or dispatch further actions
}
ImapConnectionFailedException with no clear cause.$connection = new Connection('imap.example.com', [
'ssl' => true,
'debug' => true, // Enable debug output
]);
verifyPeer => false only for testing (insecure!).authMechanism => 'XOAUTH2'.if ($message->hasLazyHeaders()) {
$message->loadHeaders(); // Force load
}
withLazyHeaders()/withLazyAttachments() are explicitly called in queries.$message = $folder->fetchByUid(1234);
$attachment->content; // Check raw content
$attachment->contentType; // Verify MIME type
Content-Disposition is parsed correctly (added in v1.17.3).$date = $message->date->toDateTime(); // Convert to Carbon
bulkQuery() throws errors on large datasets.$folder->bulkQuery()
->whereUnread()
->limit(100) // Process 100 at a time
->flag(\Flag::Seen)
->execute();
$connection = new Connection('imap.gmail.com', [
'ssl' => true,
'port' => 993,
'authMechanism' => 'XOAUTH2', // For OAuth2
'oauthUser' => 'user@example.com',
'oauthToken' => 'your_oauth_token',
]);
$connection = new Connection('imap.example.com', [
'timeout' => 60, // 60 seconds
]);
$messages = $folder->query()
->withCharset('utf-8')
->get();
How can I help you explore Laravel packages today?