Dépendances, installation & script d’exemple dans l’extrait de code ci-dessous :
<?php/** dépendances * - PHP Mime Mail Parser > https://github.com/php-mime-mail-parser/php-mime-mail-parser * - php-mailparse > https://www.php.net/manual/en/book.mailparse.php * * installation : * * sudo apt install php7.4-mailparse * composer require php-mime-mail-parser/php-mime-mail-parser */include('vendor/autoload.php');// outil$parser=newPhpMimeMailParser\Parser();// chemins$inpath=$argv[1];$outpath=str_replace('.eml','',$inpath);$pdfpath=$outpath.'.pdf';$attachmentDir=$outpath.'_att/';$parser->setPath($inpath);// headers du mailecho'from: '.$parser->getHeader('from').PHP_EOL;echo'to: '.$parser->getHeader('to').PHP_EOL;echo'subject: '.$parser->getHeader('subject').PHP_EOL;// récupération du corps du mail au format HTML, ou sinon TXT// htmlEmbedded inclut les images affichables dans le fichier HTML de sortie$body=$parser->getMessageBody('htmlEmbedded');$outpath.='.html';if(empty($body)){$body=$parser->getMessageBody('text');$outpath.='.txt';}// encodage : le fichier EML est en UTF8, il faut donc le reconvertir en ISO si le message mentionne charset=isoif(preg_match('/.*<meta.*charset=iso.*>.*/im',$body)===1){$body=utf8_decode($body);}// écriture du HTML et conversion en PDFfile_put_contents($outpath,$body);exec("wkhtmltopdf {$outpath}{$pdfpath}");// pièces-jointes excluant celles faisant partie du corps du mail grâce à l’argument false de getAttachmentsforeach($parser->getAttachments(false)as$attachment){echo'> att: '.$attachment->getFilename().' ('.$attachment->getContentType().')'.PHP_EOL;echo' ... ';$attachmentPath=$attachment->save($attachmentDir);echo'saved ('.(int)(filesize($attachmentPath)/1024).'ko)'.PHP_EOL;}// libération de la mémoireunset($parser);