Skip to content

Commit

Permalink
Merge pull request #183 from CapnFelix/strip-surrounding-single-quotes
Browse files Browse the repository at this point in the history
Trim surrounding single quotes in emails to support Outlook-style single-quotes around `To:` header weirdness
  • Loading branch information
Ocramius authored Feb 23, 2022
2 parents 76af191 + a1f2fb3 commit 1ee1a38
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static function fromString($address, $comment = null)
$email = $matches['email'];
}
$email = trim($email);
//trim single quotes, because outlook does add single quotes to emails sometimes which is technically not valid
$email = trim($email, '\'');

return new static($email, $name, $comment);
}
Expand Down
23 changes: 23 additions & 0 deletions test/Header/AddressListHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ public function getHeadersWithComments(): array
];
}

/**
* @dataProvider getHeadersWithSurroundingSingleQuotes
*/
public function testTrimSurroundingSingleQuotes(string $value): void
{
$header = To::fromString($value);
$list = $header->getAddressList();
$this->assertEquals(1, count($list));
$this->assertTrue($list->has('[email protected]'));
}

/**
* @return string[][]
*/
public function getHeadersWithSurroundingSingleQuotes(): array
{
return [
['To: <\'[email protected]\'>'],
['To: Foo Bar <\'[email protected]\'>'],
['To: \'[email protected]\''],
];
}

/**
* @group 3789
* @dataProvider getStringHeadersWithNoWhitespaceSeparator
Expand Down
14 changes: 14 additions & 0 deletions test/Storage/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception as GeneralException;
use Laminas\Mail\Exception as MailException;
use Laminas\Mail\Header\HeaderInterface;
use Laminas\Mail\Headers;
use Laminas\Mail\Storage;
use Laminas\Mail\Storage\Exception;
Expand Down Expand Up @@ -52,6 +53,17 @@ public function testGetHeader($params): void
$this->assertEquals($message->subject, 'multipart');
}

/**
* @dataProvider filesProvider
*/
public function testGetToHeader(array $params): void
{
$message = new Message($params);
/** @var HeaderInterface $toHeader */
$toHeader = $message->getHeader('To');
$this->assertEquals('[email protected]', $toHeader->getFieldValue());
}

/**
* @dataProvider filesProvider
*/
Expand Down Expand Up @@ -481,13 +493,15 @@ public function filesProvider(): array
{
$filePath = __DIR__ . '/../_files/mail.eml';
$fileBlankLineOnTop = __DIR__ . '/../_files/mail_blank_top_line.eml';
$fileSurroundingSingleQuotes = __DIR__ . '/../_files/mail_surrounding_single_quotes.eml';

return [
// Description => [params]
'resource' => [['file' => fopen($filePath, 'r')]],
'file path' => [['file' => $filePath]],
'raw' => [['raw' => file_get_contents($filePath)]],
'file with blank line on top' => [['file' => $fileBlankLineOnTop]],
'file with surrounding single quotes' => [['file' => $fileSurroundingSingleQuotes]],
];
}
}
27 changes: 27 additions & 0 deletions test/_files/mail_surrounding_single_quotes.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
To: <'[email protected]'>
Subject: multipart
Date: Sun, 01 Jan 2000 00:00:00 +0000
From: =?UTF-8?Q?"Peter M=C3=BCller"?= <[email protected]>
ContENT-type: multipart/alternative; boUNDary="crazy-multipart"
Message-ID: <CALTvGe4_oYgf9WsYgauv7qXh2-6=KbPLExmJNG7fCs9B=[email protected]>
MIME-version: 1.0

multipart message
--crazy-multipart
Content-type: text/plain
The first part
is horizontal
--crazy-multipart
Content-type: text/x-vertical
T s p i v
h e a s e
e c r r
o t t
n i
d c
a
l
--crazy-multipart--

0 comments on commit 1ee1a38

Please sign in to comment.