public function fetchMails(ImapAdapter $imap, $search, $markProcessed = true, $assume = false, \Closure $callbackFunction = null)
    {
        $folder = 'INBOX';

        $imap->selectFolder($folder);
        $result = $imap->search(array($search));

        $messages = [];

        foreach ($result as $id) {
            $message = null;

            try {
                $message = $imap->getMessage($id);

                $messages[$id]['id']     = $id;
                $messages[$id]['folder'] = $folder;

                // Zend-mail sometimes got problems, with incorrect e-mails
                try {
                    $messages[$id]['subject'] = utf8_decode($message->getHeader('subject', 'string'));
                } catch (\Exception $e) {
                    $messages[$id]['subject'] = '-No subject-';
                }

                try {
                    $messages[$id]['received'] = strtotime($message->getHeader('date', 'string'));
                } catch (\Exception $e) {
                    $messages[$id]['received'] = '-No date-';
                }

                $messages[$id]['plainText']   = $this->getPlainText($message);
                $messages[$id]['attachments'] = $this->getAttachments($message);
                $messages[$id]['type']        = $this->getTypeOfMail($messages[$id]);

                if ($assume) {
                    $messages[$id]['orderNumber'] = $this->assumeOrderNumber($messages[$id]);
                    $messages[$id]['domainName']  = $this->assumeDomainName($messages[$id]);
                }

                $success = true;
                if (is_callable($callbackFunction)) {
                    $success = $callbackFunction($id, $messages[$id]);
                }
            } catch (ExceptionInterface $e) {
                // General decoding error -> removeMessage
                unset($messages[$id]);

                // Always mark as processed
                $success = true;
            }

            if ($markProcessed && $success) {
                $this->markProcessed($imap, $id, $message);
            }
        }

        return $messages;
    }