connection->getMailbox(); try { $mailsIds = $mailbox->searchMailbox('ALL'); } catch (ConnectionException $ex) { $this->logger->error("IMAP connection failed: " . implode(",", $ex->getErrors('all'))); throw new \RuntimeException('IMAP connection failed', 0, $ex); } if (!$mailsIds) { $this->logger->info('Mailbox is empty'); return; } foreach ($mailsIds as $mailId) { $mail = $mailbox->getMail($mailId); $this->logger->debug(print_r([ 'date' => $mail->date, 'message_id' => $mail->messageId, 'from' => $mail->fromAddress, 'subject' => $mail->subject, 'hasAttachments' => $mail->hasAttachments(), ], true)); if ($this->canProcessMail($mail)) { $this->processMail($mail); $mailbox->deleteMail($mailId); $this->logger->debug("Deleted email with id=" . $mailId); } else { $this->logger->debug("Skipped mail with id=" . $mailId); } } } private function canProcessMail(IncomingMail $mail): bool { if (!$mail->hasAttachments()) { return false; } if (mb_stripos($mail->subject, $this->validEmailSubject) === false) { return false; } if (mb_stripos($mail->fromAddress, $this->validEmailSender) === false) { return false; } return true; } private function processMail(IncomingMail $mail): void { foreach ($mail->getAttachments() as $attachment) { $xmlString = $this->attachmentProcessor->extractXmlFromAttachment($attachment); if ($xmlString === null) { continue; } $xml = simplexml_load_string($xmlString); if ($xml === false) { $this->logger->error('Invalid XML from attachment'); continue; } $parsedPortfolio = $this->xmlParser->processXml($xml); if ($this->portfolioManager->updatePortfolio($parsedPortfolio, $xmlString)) { $pngFilename = tempnam(sys_get_temp_dir(), 'finfollow-portfolio-'); $this->portfolioPresenter->toImage($parsedPortfolio, $pngFilename); $this->telegramNotifier->notify('', $pngFilename); } } } }