diff --git a/Tests/mailTest.php b/Tests/mailTest.php index 919355b..d9cd29d 100644 --- a/Tests/mailTest.php +++ b/Tests/mailTest.php @@ -133,4 +133,94 @@ class mailTest extends PHPUnit_Framework_TestCase array ("name" => "", "mail" => "titi@titi.com"))); } + + /** Read all the headers and return them as JSON object + */ + public function testHeadersToJSON1 () + { + $mail = new mail (); + $mail->readMail ( // {{{ +"Return-Path: +Delivered-To: dominique@fournier38.fr +Received: from mail-postfix-mx.fournier38.fr ([192.168.1.103]) + by mail-dovecot.fournier38.fr with LMTP + id z706BqQt7V15IAAAA28uxw + (envelope-from ) + for ; Sun, 08 Dec 2019 18:06:44 +0100 +Received: from mailgw-out1.grenoble.cnrs.fr (mailgw-out1.grenoble.cnrs.fr [147.173.1.68]) + (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) + (No client certificate requested) + by mail-postfix-mx.fournier38.fr (Postfix) with ESMTPS id 8A2DF820733 + for ; Sun, 8 Dec 2019 18:06:43 +0100 (CET) +DMARC-Filter: OpenDMARC Filter v1.3.2 mail-postfix-mx.fournier38.fr 8A2DF820733 +Delivered-To: dominique.fournier@grenoble.cnrs.fr +Received: from [192.168.1.154] (82-64-55-197.subs.proxad.net [82.64.55.197]) + (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) + (No client certificate requested) + by mailgw-out1.grenoble.cnrs.fr (Postfix) with ESMTPSA id 458EDBFCE4 + for ; Sun, 8 Dec 2019 18:08:06 +0100 (CET) +From: Dominique Fournier +Subject: Analyse de texte +To: Dominique Fournier / Fournier38 +Message-ID: <81d7982f-7fee-96b4-078f-a6365234356f@grenoble.cnrs.fr> +Date: Sun, 8 Dec 2019 18:08:06 +0100 +User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 + Thunderbird/68.2.2 +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8; format=flowed +Content-Language: en-US +Content-Transfer-Encoding: 7bit +Authentication-Results: mail-postfix-mx.fournier38.fr; + dkim=none; + dmarc=none; + spf=pass (mail-postfix-mx.fournier38.fr: domain of dominique.fournier@grenoble.cnrs.fr designates 147.173.1.68 as permitted sender) smtp.mailfrom=dominique.fournier@grenoble.cnrs.fr +X-Spamd-Bar: -------------------------------------------------------------------------------------------------- + +https://openclassrooms.com/fr/courses/4470541-analysez-vos-donnees-textuelles/4854971-nettoyez-et-normalisez-les-donnees + +http://www.nltk.org/nltk_data/ + +http://www.cs.cmu.edu/~ralf/langid.html +"); +// }}} + $this->assertSame ($mail->getHeaders (), array ( + ["Return-Path" => "\n"], + ["Delivered-To" => "dominique@fournier38.fr\n"], + ["Received" => "from mail-postfix-mx.fournier38.fr ([192.168.1.103]) + by mail-dovecot.fournier38.fr with LMTP + id z706BqQt7V15IAAAA28uxw + (envelope-from ) + for ; Sun, 08 Dec 2019 18:06:44 +0100\n"], + ["Received" => "from mailgw-out1.grenoble.cnrs.fr (mailgw-out1.grenoble.cnrs.fr [147.173.1.68]) + (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) + (No client certificate requested) + by mail-postfix-mx.fournier38.fr (Postfix) with ESMTPS id 8A2DF820733 + for ; Sun, 8 Dec 2019 18:06:43 +0100 (CET)\n"], + ["DMARC-Filter" => "OpenDMARC Filter v1.3.2 mail-postfix-mx.fournier38.fr 8A2DF820733\n"], + ["Delivered-To" => "dominique.fournier@grenoble.cnrs.fr\n"], + ["Received" => "from [192.168.1.154] (82-64-55-197.subs.proxad.net [82.64.55.197]) + (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) + (No client certificate requested) + by mailgw-out1.grenoble.cnrs.fr (Postfix) with ESMTPSA id 458EDBFCE4 + for ; Sun, 8 Dec 2019 18:08:06 +0100 (CET)\n"], + ["From" => "Dominique Fournier \n"], + ["Subject" => "Analyse de texte\n"], + ["To" => "Dominique Fournier / Fournier38 \n"], + ["Message-ID" => "<81d7982f-7fee-96b4-078f-a6365234356f@grenoble.cnrs.fr>\n"], + ["Date" => "Sun, 8 Dec 2019 18:08:06 +0100\n"], + ["User-Agent" => "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 + Thunderbird/68.2.2\n"], + ["MIME-Version" => "1.0\n"], + ["Content-Type" => "text/plain; charset=utf-8; format=flowed\n"], + ["Content-Language" => "en-US\n"], + ["Content-Transfer-Encoding" => "7bit\n"], + ["Authentication-Results" => "mail-postfix-mx.fournier38.fr; + dkim=none; + dmarc=none; + spf=pass (mail-postfix-mx.fournier38.fr: domain of dominique.fournier@grenoble.cnrs.fr designates 147.173.1.68 as permitted sender) smtp.mailfrom=dominique.fournier@grenoble.cnrs.fr\n"], + ["X-Spamd-Bar" => "--------------------------------------------------------------------------------------------------\n"], + + + )); + } } diff --git a/mail.php b/mail.php index 8e433ed..2a28c19 100644 --- a/mail.php +++ b/mail.php @@ -1292,6 +1292,22 @@ class mail } // }}} + /** Get all the headers, in the order in EML. The format is + * [] => array ("header" => "value") + * @param array|null $headers The headers to examine + * @return array + */ + public function getHeaders ($headers=null) + { + $sectionMainID = $this->sectionMainID (); + if ($sectionMainID === false || + ! key_exists ("_headersArray", $this->sections[$sectionMainID])) + $headers = array (); + else + $headers = $this->sections[$sectionMainID]["_headersArray"]; + return $headers; + } + /** Get a generic header * If there is multiple headers with the same name, return the first * @param string $header The header to get @@ -1301,15 +1317,7 @@ class mail public function getHeader ($header, $headers=null) // {{{ { - if ($headers === null) - { - $sectionMainID = $this->sectionMainID (); - if ($sectionMainID === false || - ! key_exists ("_headersArray", $this->sections[$sectionMainID])) - $headers = array (); - else - $headers = $this->sections[$sectionMainID]["_headersArray"]; - } + $headers = $this->getHeaders ($headers); foreach ($headers as $key=>$val) { $head = key ($val); @@ -1330,15 +1338,7 @@ class mail public function getHeaderValue ($header, $headers=null) // {{{ { - if ($headers === null) - { - $sectionMainID = $this->sectionMainID (); - if ($sectionMainID === false || - ! key_exists ("_headersArray", $this->sections[$sectionMainID])) - $headers = array (); - else - $headers = $this->sections[$sectionMainID]["_headersArray"]; - } + $headers = $this->getHeaders ($headers); foreach ($headers as $key=>$val) { $head = key ($val);