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

imp: Import emails attachments as MessageDocuments #415

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/developers/email-collector.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ For each `MailboxEmail`:
3. it detects a potential ticket to which the email might reply;
4. if it detects a ticket, it checks that the requester can answer to it and that it is not closed;
5. otherwise it checks the requester has the permission to create tickets in the organization and it creates one based on the `Subject` and the `Body` of the email;
6. finally, it deletes the `MailboxEmail` from the database.
6. it saves attachments as `MessageDocument`s;
7. finally, it deletes the `MailboxEmail` from the database.

If anything goes wrong during this process, the error is logged in the relevant `MailboxEmail` `lastError` field.

Expand Down
9 changes: 9 additions & 0 deletions src/Entity/MailboxEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ public function getBody(): string
}
}

/**
* @return PHPIMAP\Attachment[]
*/
public function getAttachments(): array
{
$email = $this->getEmail();
return $email->getAttachments()->toArray();
}

public function getDate(): \DateTimeImmutable
{
$date = $this->getEmail()->getDate()->first();
Expand Down
35 changes: 35 additions & 0 deletions src/MessageHandler/CreateTicketsFromMailboxEmailsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
use App\Repository\MailboxRepository;
use App\Repository\MailboxEmailRepository;
use App\Repository\MessageRepository;
use App\Repository\MessageDocumentRepository;
use App\Repository\TicketRepository;
use App\Repository\UserRepository;
use App\Security\Authentication\UserToken;
use App\Security\Encryptor;
use App\Service\MessageDocumentStorage;
use App\Service\MessageDocumentStorageError;
use Psr\Log\LoggerInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
Expand All @@ -31,6 +35,8 @@ class CreateTicketsFromMailboxEmailsHandler
public function __construct(
private MailboxEmailRepository $mailboxEmailRepository,
private MessageRepository $messageRepository,
private MessageDocumentRepository $messageDocumentRepository,
private MessageDocumentStorage $messageDocumentStorage,
private TicketRepository $ticketRepository,
private UserRepository $userRepository,
private AccessDecisionManagerInterface $accessDecisionManager,
Expand Down Expand Up @@ -119,6 +125,35 @@ public function __invoke(CreateTicketsFromMailboxEmails $message): void

$this->ticketRepository->save($ticket, true);
$this->messageRepository->save($message, true);

foreach ($mailboxEmail->getAttachments() as $attachment) {
$tmpPath = sys_get_temp_dir();
$filename = $attachment->getName();
$status = $attachment->save($tmpPath, $filename);

if (!$status) {
$this->logger->warning(
"MailboxEmail {$mailboxEmail->getId()} cannot import {$filename}: can't save in temporary dir"
);
continue;
}

$filepath = $tmpPath . '/' . $filename;
$file = new File($filepath, false);

try {
$messageDocument = $this->messageDocumentStorage->store($file, $filename);
} catch (MessageDocumentStorageError $e) {
$this->logger->warning(
"MailboxEmail {$mailboxEmail->getId()} cannot import {$filename}: {$e->getMessage()}"
);
continue;
}

$messageDocument->setMessage($message);
$this->messageDocumentRepository->save($messageDocument, true);
}

$this->mailboxEmailRepository->remove($mailboxEmail, true);

$this->bus->dispatch(new SendMessageEmail($message->getId()));
Expand Down