feat(mail) : intégration mail OVH IMAP — boîte partagée, lecture, création/lien tâche #5
@@ -17,6 +17,7 @@ use SodiumException;
|
|||||||
use Throwable;
|
use Throwable;
|
||||||
use Webklex\PHPIMAP\Client;
|
use Webklex\PHPIMAP\Client;
|
||||||
use Webklex\PHPIMAP\ClientManager;
|
use Webklex\PHPIMAP\ClientManager;
|
||||||
|
use Webklex\PHPIMAP\IMAP;
|
||||||
|
|
||||||
final class ImapMailProvider implements MailProviderInterface
|
final class ImapMailProvider implements MailProviderInterface
|
||||||
{
|
{
|
||||||
@@ -90,13 +91,19 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('listMessages', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('listMessages', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$messages = $folder->query()->leaveUnread()->get();
|
$messages = $folder->query()
|
||||||
|
->whereAll()
|
||||||
|
->setFetchBody(false)
|
||||||
|
->leaveUnread()
|
||||||
|
->setSequence(IMAP::ST_UID)
|
||||||
|
->get()
|
||||||
|
;
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
$items = array_slice($messages->toArray(), $offset, $limit);
|
$items = array_slice($messages->toArray(), $offset, $limit);
|
||||||
|
|
||||||
foreach ($items as $message) {
|
foreach ($items as $message) {
|
||||||
$result[] = $this->buildHeaderDto($message);
|
$result[] = $this->buildHeaderDto($message, withSnippet: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
$client->disconnect();
|
$client->disconnect();
|
||||||
@@ -121,7 +128,7 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('fetchMessage', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('fetchMessage', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $folder->query()->uid($uid)->leaveUnread()->get()->first();
|
$message = $folder->query()->uid($uid)->leaveUnread()->setSequence(IMAP::ST_UID)->get()->first();
|
||||||
|
|
||||||
if (null === $message) {
|
if (null === $message) {
|
||||||
throw MailProviderException::operationFailed('fetchMessage', sprintf('UID %d not found in folder %s', $uid, $folderPath));
|
throw MailProviderException::operationFailed('fetchMessage', sprintf('UID %d not found in folder %s', $uid, $folderPath));
|
||||||
@@ -168,7 +175,7 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('markRead', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('markRead', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $folder->query()->uid($uid)->leaveUnread()->get()->first();
|
$message = $folder->query()->uid($uid)->leaveUnread()->setSequence(IMAP::ST_UID)->get()->first();
|
||||||
|
|
||||||
if (null === $message) {
|
if (null === $message) {
|
||||||
throw MailProviderException::operationFailed('markRead', sprintf('UID %d not found', $uid));
|
throw MailProviderException::operationFailed('markRead', sprintf('UID %d not found', $uid));
|
||||||
@@ -200,7 +207,7 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('markFlagged', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('markFlagged', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $folder->query()->uid($uid)->leaveUnread()->get()->first();
|
$message = $folder->query()->uid($uid)->leaveUnread()->setSequence(IMAP::ST_UID)->get()->first();
|
||||||
|
|
||||||
if (null === $message) {
|
if (null === $message) {
|
||||||
throw MailProviderException::operationFailed('markFlagged', sprintf('UID %d not found', $uid));
|
throw MailProviderException::operationFailed('markFlagged', sprintf('UID %d not found', $uid));
|
||||||
@@ -232,7 +239,7 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('moveMessage', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('moveMessage', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $folder->query()->uid($uid)->leaveUnread()->get()->first();
|
$message = $folder->query()->uid($uid)->leaveUnread()->setSequence(IMAP::ST_UID)->get()->first();
|
||||||
|
|
||||||
if (null === $message) {
|
if (null === $message) {
|
||||||
throw MailProviderException::operationFailed('moveMessage', sprintf('UID %d not found', $uid));
|
throw MailProviderException::operationFailed('moveMessage', sprintf('UID %d not found', $uid));
|
||||||
@@ -259,7 +266,7 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
throw MailProviderException::operationFailed('fetchAttachment', sprintf('Folder %s not found', $folderPath));
|
throw MailProviderException::operationFailed('fetchAttachment', sprintf('Folder %s not found', $folderPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = $folder->query()->uid($uid)->leaveUnread()->get()->first();
|
$message = $folder->query()->uid($uid)->leaveUnread()->setSequence(IMAP::ST_UID)->get()->first();
|
||||||
|
|
||||||
if (null === $message) {
|
if (null === $message) {
|
||||||
throw MailProviderException::operationFailed('fetchAttachment', sprintf('UID %d not found', $uid));
|
throw MailProviderException::operationFailed('fetchAttachment', sprintf('UID %d not found', $uid));
|
||||||
@@ -331,11 +338,11 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
return $client;
|
return $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildHeaderDto(mixed $message): MailMessageHeaderDto
|
private function buildHeaderDto(mixed $message, bool $withSnippet = true): MailMessageHeaderDto
|
||||||
{
|
{
|
||||||
$from = $message->getFrom()->first();
|
$from = $message->getFrom()->first();
|
||||||
$fromAddress = null !== $from ? (string) $from->mail : '';
|
$fromAddress = null !== $from ? (string) $from->mail : '';
|
||||||
$fromName = null !== $from ? ($from->personal ?? null) : null;
|
$fromName = null !== $from && null !== $from->personal ? (string) $from->personal : null;
|
||||||
|
|
||||||
$toAddresses = [];
|
$toAddresses = [];
|
||||||
foreach ($message->getTo() as $addr) {
|
foreach ($message->getTo() as $addr) {
|
||||||
@@ -351,18 +358,28 @@ final class ImapMailProvider implements MailProviderInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$sentAt = $message->getDate()?->toDateTimeImmutable() ?? new DateTimeImmutable();
|
$sentAt = new DateTimeImmutable();
|
||||||
|
$dateAttr = $message->getDate();
|
||||||
|
if (null !== $dateAttr) {
|
||||||
|
try {
|
||||||
|
$sentAt = DateTimeImmutable::createFromInterface($dateAttr->toDate());
|
||||||
|
} catch (Throwable) {
|
||||||
|
// keep default when the header date is missing or unparsable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$snippet = null;
|
$snippet = null;
|
||||||
|
if ($withSnippet) {
|
||||||
$text = $message->getTextBody();
|
$text = $message->getTextBody();
|
||||||
if (null !== $text && '' !== $text) {
|
if (null !== $text && '' !== $text) {
|
||||||
$snippet = mb_substr(strip_tags($text), 0, 200);
|
$snippet = mb_substr(strip_tags($text), 0, 200);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new MailMessageHeaderDto(
|
return new MailMessageHeaderDto(
|
||||||
uid: (int) $message->getUid(),
|
uid: (int) $message->getUid(),
|
||||||
messageId: (string) $message->getMessageId(),
|
messageId: (string) $message->getMessageId(),
|
||||||
subject: $message->getSubject() ?: null,
|
subject: '' !== (string) $message->getSubject() ? (string) $message->getSubject() : null,
|
||||||
fromAddress: $fromAddress,
|
fromAddress: $fromAddress,
|
||||||
fromName: $fromName,
|
fromName: $fromName,
|
||||||
toAddresses: $toAddresses,
|
toAddresses: $toAddresses,
|
||||||
|
|||||||
Reference in New Issue
Block a user