Skip to content

Parse date in MailCollector GLPI 11.0.1 #21610

@jobercavalcante

Description

@jobercavalcante

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Version

GLPI 11.0.1 Copyright (C) 2015-2025 Teclib' and contributors

Bug description

🐛 Erro ao processar cabeçalho de e-mail com fuso horário duplicado / Error when processing email header with duplicated timezone

Versão / Version: GLPI 11.0.1 Copyright (C) 2015-2025 Teclib' and contributors
Arquivo / File: src/MailCollector.php
Linha aproximada / Approx. line: 1364
Função / Function: MailCollector->getHeaders()
Tipo de problema / Label suggestion: bug, mailgate, datetime


🇬🇧 Description (English)

When running mail collection (cronMailgate), GLPI throws:

Safe\Exceptions\DatetimeException: An error occurred

This happens when the Date: header contains two timezone definitions, for example:

Date: Wed, 22 Oct 2025 11:44:56 -0400 (GMT-04:00)

The PHP strtotime() function (used by Safe\strtotime()) cannot parse this format and fails, stopping the entire mail collection.

🧩 Root Cause

The extra (GMT-04:00) at the end of the header creates ambiguity in timezone parsing, resulting in an invalid date.

✅ Suggested Fix

Clean the header before parsing:

$raw_date = $message->getHeader('Date', 'string');
$clean_date = preg_replace('/\s*\(.*\)$/', '', trim($raw_date));
$timestamp = strtotime($clean_date) ?: time();
$date = date("Y-m-d H:i:s", $timestamp);

This prevents exceptions and ensures mail collection continues smoothly even with redundant timezone annotations.


🧾 Example comparison

Header Result Notes
Date: Wed, 22 Oct 2025 17:19:22 +0000 ✅ Works Standard RFC 2822 format
Date: Wed, 22 Oct 2025 11:44:56 -0400 (GMT-04:00) ❌ Fails Duplicated timezone

💡 Additional note

A simple patch in MailCollector.php prevents the error and logs invalid headers for later analysis without breaking the mail collection process.


🔧 Optional patch example (.diff)

--- a/src/MailCollector.php
+++ b/src/MailCollector.php
@@ -1360,7 +1360,22 @@
     // Old code
-    $date = date("Y-m-d H:i:s", strtotime($message->getHeader('Date', 'string')));
+    try {
+        $raw_date = $message->getHeader('Date', 'string');
+
+        // Sanitize timezone duplicates like "(GMT-04:00)" or "(AMT)"
+        $clean_date = preg_replace('/\s*\(.*\)$/', '', trim($raw_date));
+
+        // Try to parse the cleaned date
+        $timestamp = @strtotime($clean_date);
+
+        // Fallback if parsing fails
+        if ($timestamp === false) {
+            throw new \Exception("Invalid date header: " . $raw_date);
+        }
+
+        $date = date("Y-m-d H:i:s", $timestamp);
+    } catch (\Throwable $e) {
+        $date = date("Y-m-d H:i:s");
+        Toolbox::logDebug("MailCollector: Invalid Date header '{$raw_date}' - using current timestamp.");
+    }

Relevant log output

Page URL

No response

Steps To reproduce

No response

Your GLPI setup information

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions