Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to fix memory issue when loading message? #516

Open
cris-m opened this issue Sep 23, 2024 · 4 comments
Open

how to fix memory issue when loading message? #516

cris-m opened this issue Sep 23, 2024 · 4 comments

Comments

@cris-m
Copy link

cris-m commented Sep 23, 2024

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?

@Drackokacka
Copy link

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.

@cris-m
Copy link
Author

cris-m commented Oct 2, 2024

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

@bjaverhagen
Copy link

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.

@cris-m
Copy link
Author

cris-m commented Oct 14, 2024

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'));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants