Skip to content

Can't open log file with "embedded" mail attachment #4

@pzoechner

Description

@pzoechner

Hi,

I am experiencing an issue where the log file can't be opened - "No results" and an error message below the search field: "Request failed with status code 500: Undefined array key 1" - after a mail with embedded files (images) was logged. I'm using Laravel's $message->embed() function (Docs) to attach files to my email.

local.ERROR: Undefined array key 1 {"exception":"[object] (ErrorException(code: 0): Undefined array key 1 at /[...]/vendor/opcodesio/mail-parser/src/Message.php:180)

I am using

  • PHP 8.5.2
  • Laravel 12
  • log-viewer v3.21.1
  • mail-parser v0.1.6

I asked Claude Code (Opus 4.5) for a quick explanation. I'm not saying that the explanation below is correct in its assessment or its solution is great (e.g. nested tertiary statements are deprecated since PHP 7.4). Maybe it can be a helpful pointer to fixing the bug.

The bug is at line 180:

$currentBodyHeaders[$currentBodyHeaderInProgress] .= PHP_EOL . $line;

The issue is that $currentBodyHeaderInProgress can be set to true (a boolean) instead of a string key. Look at line 181:

$currentBodyHeaderInProgress = str_ends_with($currentBodyHeaders[$currentBodyHeaderInProgress], ';');

This sets $currentBodyHeaderInProgress to true or false (the return value of str_ends_with), but then on the next iteration, line 180 tries to use it as an array key with $currentBodyHeaders[$currentBodyHeaderInProgress], which causes $currentBodyHeaders[1] (since true is cast to 1) - hence "Undefined array key 1".

The same bug exists at line 175 for $headerInProgress.

This is a bug in the opcodesio/mail-parser vendor package, not in your code. The fix should be:

// Line 175 should be:
$headerInProgress = str_ends_with($this->headers[$headerInProgress], ';') ? $headerInProgress : null;

// Line 181 should be:
$currentBodyHeaderInProgress = str_ends_with($currentBodyHeaders[$currentBodyHeaderInProgress], ';') ? $currentBodyHeaderInProgress : null;

When I applied the AI suggested fix, it didn't display the HTML mail correctly (there was the text representation of the mail displayed where the first logo/attachment should have been) but at least I was able to open and view the log file in log-viewer again 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions