Skip to content

Issue when parsing header if the from field contains a semicolon #300

Open
@nilshellerhoff

Description

@nilshellerhoff

Describe the bug
When parsing the header of an email where the from field contains a semicolon ";", the from field will not be parsed correctly. Minimal example of such an email:

To: somemail@domain.tld
Subject: Test of a semicolon in from-header
Date: Wed, 12 Oct 2022 16:31:06 +0000
From: "Foo; Bar" <foobar@domain.tld>
Message-ID: <asdf@domain.tld>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

<< email body >>

The raw email is appended to avoid issues with linebreaks (I changed the extension to .txt as Github doesn't support .eml).
semicolon_test.txt

Used config
Default.

Code to Reproduce

$raw_mail = file_get_contents('semicolon_test.txt');
$header = new \Webklex\PHPIMAP\Header($raw_mail);
var_dump($header->get('from'));

Output when running this via php test.php:

PHP Warning:  Trying to access array offset on value of type null in ***/vendor/webklex/php-imap/src/Header.php on line 457
PHP Warning:  Trying to access array offset on value of type null in ***/vendor/webklex/php-imap/src/Header.php on line 457
object(Webklex\PHPIMAP\Attribute)#18 (2) {
  ["name":protected]=>
  string(4) "from"
  ["values":protected]=>
  array(1) {
    [0]=>
    string(4) ""Foo"
  }
}

Expected behavior
When we remove the semicolon from the from-header, we get the expected result:

PHP Warning:  Trying to access array offset on value of type null in ***/vendor/webklex/php-imap/src/Header.php on line 457
PHP Warning:  Trying to access array offset on value of type null in ***/vendor/webklex/php-imap/src/Header.php on line 457
object(Webklex\PHPIMAP\Attribute)#6 (2) {
  ["name":protected]=>
  string(4) "from"
  ["values":protected]=>
  array(1) {
    [0]=>
    object(Webklex\PHPIMAP\Address)#7 (5) {
      ["personal"]=>
      string(9) ""Foo Bar""
      ["mailbox"]=>
      string(6) "foobar"
      ["host"]=>
      string(10) "domain.tld"
      ["mail"]=>
      string(17) "foobar@domain.tld"
      ["full"]=>
      string(29) ""Foo Bar" <foobar@domain.tld>"
    }
  }
}

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop / Server (please complete the following information):

  • OS: Linux Mint 21 (= Ubuntu 22.04)

  • PHP: 7.4 and 8.1

  • Version 4.0.2

  • Provider: The mail which triggered the issue was sent through the "Contact Form 7" plugin on a Wordpress instance.

EDIT:
I am not actually sure that a semicolon in the header fields confroms to the spec, but Gmail, Thunderbird and also phpmailer do handle these mails correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions