feat(mail) : décodage des en-têtes MIME + aperçu inline des pièces jointes
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
- Décode les encoded-words MIME (RFC 2047) des sujets et noms d'expéditeur via App\Mail\MimeHeaderDecoder, appliqué dans ImapMailProvider (sync propre) - Commande app:mail:redecode-headers (--dry-run) pour re-décoder l'existant en base - Aperçu inline images + PDF en visionneuse modale plein écran (MailAttachmentPreview), téléchargement conservé pour les autres types - Tests unitaires du décodeur + maj docs/mail-integration.md Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
84
src/Command/MailRedecodeHeadersCommand.php
Normal file
84
src/Command/MailRedecodeHeadersCommand.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Mail\MimeHeaderDecoder;
|
||||
use App\Repository\MailMessageRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:mail:redecode-headers',
|
||||
description: 'Re-décode les sujets et noms d\'expéditeur encodés en MIME (RFC 2047) déjà stockés en base',
|
||||
)]
|
||||
final class MailRedecodeHeadersCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MailMessageRepository $messageRepository,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addOption(
|
||||
'dry-run',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Affiche les changements sans écrire en base',
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$isDryRun = (bool) $input->getOption('dry-run');
|
||||
|
||||
$messages = $this->messageRepository->findAll();
|
||||
$io->text(sprintf('%d message(s) à examiner...', count($messages)));
|
||||
|
||||
$changed = 0;
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$newSubject = MimeHeaderDecoder::decode($message->getSubject());
|
||||
$newFromName = MimeHeaderDecoder::decode($message->getFromName());
|
||||
|
||||
$hasChange = $newSubject !== $message->getSubject() || $newFromName !== $message->getFromName();
|
||||
|
||||
if (!$hasChange) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($io->isVerbose()) {
|
||||
$io->text(sprintf(' - #%d : "%s" → "%s"', $message->getId(), (string) $message->getSubject(), (string) $newSubject));
|
||||
}
|
||||
|
||||
if (!$isDryRun) {
|
||||
$message->setSubject($newSubject);
|
||||
$message->setFromName($newFromName);
|
||||
}
|
||||
|
||||
++$changed;
|
||||
}
|
||||
|
||||
if (!$isDryRun) {
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$io->success(sprintf(
|
||||
'%s%d en-tête(s) re-décodé(s).',
|
||||
$isDryRun ? '[dry-run] ' : '',
|
||||
$changed,
|
||||
));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -351,7 +351,9 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
{
|
||||
$from = $message->getFrom()->first();
|
||||
$fromAddress = null !== $from ? (string) $from->mail : '';
|
||||
$fromName = null !== $from && null !== $from->personal ? (string) $from->personal : null;
|
||||
$fromName = null !== $from && null !== $from->personal
|
||||
? MimeHeaderDecoder::decode((string) $from->personal)
|
||||
: null;
|
||||
|
||||
$toAddresses = [];
|
||||
foreach ($message->getTo() as $addr) {
|
||||
@@ -388,7 +390,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
return new MailMessageHeaderDto(
|
||||
uid: (int) $message->getUid(),
|
||||
messageId: (string) $message->getMessageId(),
|
||||
subject: '' !== (string) $message->getSubject() ? (string) $message->getSubject() : null,
|
||||
subject: '' !== (string) $message->getSubject() ? MimeHeaderDecoder::decode((string) $message->getSubject()) : null,
|
||||
fromAddress: $fromAddress,
|
||||
fromName: $fromName,
|
||||
toAddresses: $toAddresses,
|
||||
|
||||
47
src/Mail/MimeHeaderDecoder.php
Normal file
47
src/Mail/MimeHeaderDecoder.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use const ICONV_MIME_DECODE_CONTINUE_ON_ERROR;
|
||||
|
||||
/**
|
||||
* Décode les en-têtes mail encodés en « encoded-words » MIME (RFC 2047),
|
||||
* ex: "=?UTF-8?Q?Fwd=3A_Votre_inscription?=" → "Fwd: Votre inscription".
|
||||
*
|
||||
* Certains serveurs IMAP (OVH) renvoient les sujets / noms d'expéditeur
|
||||
* encodés bruts ; webklex ne les décode pas systématiquement. Cet utilitaire
|
||||
* normalise la sortie en UTF-8 lisible. Idempotent : un texte déjà décodé
|
||||
* (sans séquence "=?") est retourné inchangé.
|
||||
*/
|
||||
final class MimeHeaderDecoder
|
||||
{
|
||||
public static function decode(?string $value): ?string
|
||||
{
|
||||
if (null === $value || '' === $value) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Pas d'encoded-word → rien à faire (chemin rapide + idempotence).
|
||||
if (!str_contains($value, '=?')) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$decoded = @iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
|
||||
|
||||
if (false === $decoded || '' === trim($decoded)) {
|
||||
// Fallback : mb_decode_mimeheader gère certains cas refusés par iconv.
|
||||
$previous = mb_internal_encoding();
|
||||
mb_internal_encoding('UTF-8');
|
||||
|
||||
try {
|
||||
$decoded = mb_decode_mimeheader($value);
|
||||
} finally {
|
||||
mb_internal_encoding($previous);
|
||||
}
|
||||
}
|
||||
|
||||
return false === $decoded || '' === $decoded ? $value : $decoded;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user