Skip to content

how to fix memory issue when loading message? #516

Open
@cris-m

Description

I was trying to load emails. it work on other email but I get issue with email account with multiple messages.

here is the code:

 $config = config('imap.accounts.default');
$config['username'] = $credentials['email'];
$config['password'] = $credentials['password'];

$client = Client::make($config);
$folders = [];
$mailFolders = $client->getFolders($hierarchical = false);

foreach ($mailFolders as $folder) {
    $folder_name = $folder->name;
    
    $folders[$folder_name]['unread_count'] = $folder->messages()->unseen()->count();
    $folders[$folder_name]['messages'] = [];

    $folder->messages()->all()->chunked(function ($messages, $chunk) use ($folder_name, &$folders) {
        $messages->each(function($message) use ($folder_name, &$folders) {
            $folders[$folder_name]['messages'][] = $message;
        });
    }, $chunk_size = 10, $start_chunk = 1);
}

I get the following issue in laravel:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1576960 bytes) in vendor/webklex/php-imap/src/Part.php on line 196

Is there a better way to load all message?

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions