From 222fc34dcc3ab7c636ac1f743afd1666dbc8f54f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Thu, 16 Jul 2020 09:14:08 +0200 Subject: [PATCH] Handle missing 'to' email header --- inc/mailcollector.class.php | 14 ++++++++------ tests/emails-tests/12-missing-to.eml | 28 ++++++++++++++++++++++++++++ tests/imap/MailCollector.php | 5 +++-- 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 tests/emails-tests/12-missing-to.eml diff --git a/inc/mailcollector.class.php b/inc/mailcollector.class.php index c76a7c15014..a397124b3fd 100644 --- a/inc/mailcollector.class.php +++ b/inc/mailcollector.class.php @@ -1369,14 +1369,16 @@ function getHeaders(\Laminas\Mail\Storage\Message $message) { $mail_details = []; // Construct to and cc arrays - $h_tos = $message->getHeader('to'); $tos = []; - foreach ($h_tos->getAddressList() as $address) { - $mailto = Toolbox::strtolower($address->getEmail()); - if ($mailto === $this->fields['name']) { - $to = $mailto; + if (isset($message->to)) { + $h_tos = $message->getHeader('to'); + foreach ($h_tos->getAddressList() as $address) { + $mailto = Toolbox::strtolower($address->getEmail()); + if ($mailto === $this->fields['name']) { + $to = $mailto; + } + $tos[] = $mailto; } - $tos[] = $mailto; } $ccs = []; diff --git a/tests/emails-tests/12-missing-to.eml b/tests/emails-tests/12-missing-to.eml new file mode 100644 index 00000000000..27def61a3eb --- /dev/null +++ b/tests/emails-tests/12-missing-to.eml @@ -0,0 +1,28 @@ +Return-Path: tech@glpi-project.org +Received: from 192.168.1.3 (LHLO mail.glpi-project.org) (192.168.1.3) + by mail.glpi-project.org with LMTP; Tue, 2 Apr 2019 14:43:12 +0200 + (CEST) +Received: from mail.glpi-project.org (localhost [127.0.0.1]) + by mail.glpi-project.org (Postfix) with ESMTP id 6F3457E80D1E + for ; Tue, 2 Apr 2019 14:43:12 +0200 (CEST) +Received: from localhost (localhost [127.0.0.1]) + by mail.glpi-project.org (Postfix) with ESMTP id 645C77E80D1C + for ; Tue, 2 Apr 2019 14:43:12 +0200 (CEST) +Received: from mail.glpi-project.org ([127.0.0.1]) + by localhost (mail.glpi-project.org [127.0.0.1]) (amavisd-new, port 10026) + with ESMTP id POJTfrC8TVrL for ; + Tue, 2 Apr 2019 14:43:12 +0200 (CEST) +Received: from mail.glpi-project.org (localhost [127.0.0.1]) + by mail.glpi-project.org (Postfix) with ESMTP id 55AF37E80C20 + for ; Tue, 2 Apr 2019 14:43:12 +0200 (CEST) +Date: Tue, 2 Apr 2019 14:43:12 +0200 (CEST) +From: Tech Ni Cian +Cci: GLPI debug +Message-ID: <2078889367.1658939.1554208992329.JavaMail.zimbra@glpi-project.org> +Subject: A message without to header +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: 7bit + +This message was sent to multiple mailboxes using cci header. + diff --git a/tests/imap/MailCollector.php b/tests/imap/MailCollector.php index c2be5b87bd2..43848e38a2b 100644 --- a/tests/imap/MailCollector.php +++ b/tests/imap/MailCollector.php @@ -258,7 +258,7 @@ public function testCollect() { $this->doConnect(); $this->collector->maxfetch_emails = 1000; // Be sure to fetch all mails from test suite $msg = $this->collector->collect($this->mailgate_id); - $this->variable($msg)->isIdenticalTo('Number of messages: available=12, retrieved=12, refused=2, errors=1, blacklisted=0'); + $this->variable($msg)->isIdenticalTo('Number of messages: available=13, retrieved=13, refused=2, errors=1, blacklisted=0'); $rejecteds = iterator_to_array($DB->request(['FROM' => \NotImportedEmail::getTable()])); $this->array($rejecteds)->hasSize(2); @@ -286,7 +286,7 @@ public function testCollect() { ] ]); - $this->integer(count($iterator))->isIdenticalTo(4); + $this->integer(count($iterator))->isIdenticalTo(5); $names = []; while ($data = $iterator->next()) { $names[] = $data['name']; @@ -297,6 +297,7 @@ public function testCollect() { 'Re: [GLPI #0001155] New ticket database issue', 'Ticket with observer', 'Re: [GLPI #0038927] Update - Issues with new Windows 10 machine', + 'A message without to header', ]; $this->array($names)->isIdenticalTo($expected_names);