php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

how to fix memory issue when loading message?

Open cris-m opened this issue 1 year ago • 4 comments

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?

cris-m avatar Sep 23 '24 10:09 cris-m

If you don't need to access older messages, you could use folder's method since to get smaller batch of messages, see example:

$messages = $folder->query()
    ->since('01.01.2024')
    ->all();

I hope someone will come with better solution to read ALL messages in chunks without heavy memory impact.

Drackokacka avatar Sep 30 '24 07:09 Drackokacka

I still getting the same error. if there a way to handle an account with multiple messages?

cris-m avatar Oct 02 '24 12:10 cris-m

I had a memory problem when fetching messages with large attachments? I fixed the memory-issue with this solution: #457 Maybe it can help you to.

bjaverhagen avatar Oct 13 '24 10:10 bjaverhagen

I had to increase memory_limit in php.ini to 512M but loading all the message throw :

Webklex\ PHPIMAP\ Exceptions\ GetMessagesFailedException
Trailing data

I tried the following:

 public function index(Request $request)
    {
        $credentials = decrypt(session('imap_credentials'));
        $config = config('imap.accounts.default');
        $config['username'] = $credentials['email'];
        $config['password'] = $credentials['password'];

        $client = Client::make($config);

        if (!$client->isConnected()) {
            try {
                $client->checkConnection();
            } catch (\Exception $e) {
                Auth::logout();
                $request->session()->invalidate();

                return redirect()->route('login')->with('alert', [
                    'type' => 'danger',
                    'message' => 'Failed to connect to the email server. You have been logged out. Please log in again.',
                ]);
            }
        }

        $folders = [];
        $mailFolders = $client->getFolders($hierarchical = false);

        foreach ($mailFolders as $folder) {
            $folder_name = $folder->name;
            
            // Get unread message count
            $folders[$folder_name]['unread_count'] = $folder->messages()->unseen()->count();
            
            // Fetch messages from the folder
            $folders[$folder_name]['messages'] = $folder->messages()->all()->fetchOrderDesc()->get();
        }
        
        return view('office.emails.test', compact('folders'));
    }

cris-m avatar Oct 14 '24 04:10 cris-m