Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing contenttype in collected emails #7688

Merged
merged 4 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions inc/mailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2019,11 +2019,19 @@ public function getDecodedContent(\Laminas\Mail\Storage\Part $part) {
break;
}

$contentType = $part->getHeader('contentType');
if ($contentType instanceof \Laminas\Mail\Header\ContentType
&& preg_match('/^text\//', $contentType->getType())
&& mb_detect_encoding($contents) != 'UTF-8') {
$contents = Toolbox::encodeInUtf8($contents, $contentType->getEncoding());
try {
$contentTypePart = $part->getHeader('contentType');
$contentType = $contentTypePart->getType();
} catch (\Laminas\Mail\Storage\Exception\InvalidArgumentException $e) {
//no ContentType header, switch to acceptable default
$contentType = "text/plain";
} finally {
if (preg_match('/^text\//', $contentType) && ($encoding = mb_detect_encoding($contents)) != 'UTF-8') {
$contents = Toolbox::encodeInUtf8(
$contents,
(isset($contentTypePart) ? $contentTypePart->getEncoding() : $encoding)
);
}
}

return $contents;
Expand Down
37 changes: 37 additions & 0 deletions tests/emails-tests/11-missing-contenttype.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Delivered-To: unittests@glpi-project.org
Received: by 2002:a50:cfc3:0:0:0:0:0 with SMTP id i3csp3500637edk;
Tue, 14 Jul 2020 05:17:41 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJzNg81xPyGu0K/t2h9jrfIGP7f8arkPuRpR2dTImwUbptUDs7PwZMnVw+TGDnGflCsyHywJ
X-Received: by 2002:a17:906:84ef:: with SMTP id zp15mr4233633ejb.3.1594729061639;
Tue, 14 Jul 2020 05:17:41 -0700 (PDT)
Return-Path: <ces+bncBDY5VFVI7MJBBZGEW34AKGQE65R3V3I@glpi-project.org>
Received: from mail.glpi-project.org (mail.glpi-project.org. [127.0.0.1])
by mx.google.com with ESMTPS id yh22si12729797ejb.62.2020.07.14.05.17.41
for <unittests@glpi-project.org>
(version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
Tue, 14 Jul 2020 05:17:41 -0700 (PDT)
Received-SPF: pass (google.com: domain of ces+bncbdy5vfvi7mjbbzgew34akgqe65r3v3i@glpi-project.org designates 127.0.0.1 as permitted sender) client-ip=127.0.0.1;
Received: from mail-ej1-f71.google.com ([127.0.0.1])
by mail.glpi-project.org with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jul 2020 13:17:41 +0100
Received: by mail-ej1-f71.google.com with SMTP id op28sf22019349ejb.15
for <unittests@glpi-project.org>; Tue, 14 Jul 2020 05:17:41 -0700 (PDT)
X-Received: by 2002:a50:e385:: with SMTP id b5mr4124200edm.130.1594729061079;
Tue, 14 Jul 2020 05:17:41 -0700 (PDT)
X-Received: by 2002:a50:e385:: with SMTP id b5mr4124184edm.130.1594729060866;
Tue, 14 Jul 2020 05:17:40 -0700 (PDT)
MIME-Version: 1.0
Received: by 2002:a17:906:1fd7:: with SMTP id e23ls8936851ejt.7.gmail; Tue, 14
Jul 2020 05:17:40 -0700 (PDT)
X-Received: by 2002:a17:906:774d:: with SMTP id o13mr4133448ejn.373.1594729060159;
Tue, 14 Jul 2020 05:17:40 -0700 (PDT)
Received: from mail.glpi-project.org (mail.glpi-project.org. [127.0.0.1])
by mx.google.com with SMTP id h24si5438068edj.24.2020.07.14.05.17.39
for <unittests@glpi-project.org>;
Tue, 14 Jul 2020 05:17:40 -0700 (PDT)
Date: 14 Jul 2020 13:17:37 +0100
Message-Id: <5021fe$691cb2b=d78b1276125ef06c@mail.glpi-project.org>
From: Normal User <normal@glpi-project.org>
To: unittests@glpi-project.org
Subject: No contenttype

An email without contenttype...
5 changes: 3 additions & 2 deletions tests/imap/MailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=11, retrieved=11, refused=2, errors=1, blacklisted=0');
$this->variable($msg)->isIdenticalTo('Number of messages: available=12, retrieved=12, refused=2, errors=1, blacklisted=0');
$rejecteds = iterator_to_array($DB->request(['FROM' => \NotImportedEmail::getTable()]));

$this->array($rejecteds)->hasSize(2);
Expand Down Expand Up @@ -318,7 +318,7 @@ public function testCollect() {
]
]);

$this->integer(count($iterator))->isIdenticalTo(4);
$this->integer(count($iterator))->isIdenticalTo(5);
$names = [];
while ($data = $iterator->next()) {
$names[] = $data['name'];
Expand All @@ -329,6 +329,7 @@ public function testCollect() {
'Test images',
'Test\'ed issue',
'Test Email from Outlook',
'No contenttype'
];
$this->array($names)->isIdenticalTo($expected_names);

Expand Down