From a6f59d414c4ba12aff6fe97ffc00213b2e6c328a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 5 Aug 2020 23:08:45 +0700 Subject: [PATCH 1/3] Fixes #19 Fixes #72 duplicate subject header on windows Signed-off-by: Abdul Malik Ikhsan --- src/Transport/Sendmail.php | 7 +----- test/Transport/SendmailTest.php | 41 ++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Transport/Sendmail.php b/src/Transport/Sendmail.php index 3223b3fa..98ee2142 100644 --- a/src/Transport/Sendmail.php +++ b/src/Transport/Sendmail.php @@ -222,12 +222,7 @@ protected function prepareBody(Mail\Message $message) */ protected function prepareHeaders(Mail\Message $message) { - // On Windows, simply return verbatim - if ($this->isWindowsOs()) { - return $message->getHeaders()->toString(); - } - - // On *nix platforms, strip the "to" header + // Strip the "to" and "subject" headers $headers = clone $message->getHeaders(); $headers->removeHeader('To'); $headers->removeHeader('Subject'); diff --git a/test/Transport/SendmailTest.php b/test/Transport/SendmailTest.php index abdd3787..3b740065 100644 --- a/test/Transport/SendmailTest.php +++ b/test/Transport/SendmailTest.php @@ -28,6 +28,7 @@ class SendmailTest extends TestCase public $message; public $additional_headers; public $additional_parameters; + public $operating_system; public function setUp() { @@ -72,9 +73,14 @@ public function getMessage() return $message; } + public function isWindows() + { + return $this->operating_system === 'WIN'; + } + public function testReceivesMailArtifactsOnUnixSystems() { - if ($this->operating_system == 'WIN') { + if ($this->isWindows()) { $this->markTestSkipped('This test is *nix-specific'); } @@ -96,7 +102,7 @@ public function testReceivesMailArtifactsOnUnixSystems() public function testReceivesMailArtifactsOnWindowsSystems() { - if ($this->operating_system != 'WIN') { + if (! $this->isWindows()) { $this->markTestSkipped('This test is Windows-specific'); } @@ -120,7 +126,7 @@ public function testReceivesMailArtifactsOnWindowsSystems() public function testLinesStartingWithFullStopsArePreparedProperlyForWindows() { - if ($this->operating_system != 'WIN') { + if (! $this->isWindows()) { $this->markTestSkipped('This test is Windows-specific'); } @@ -255,4 +261,33 @@ public function testDoNotAllowMessageWithoutToAndCcAndBccHeaders() $this->expectException(RuntimeException::class); $this->transport->send($message); } + + /** + * @see @see https://github.com/laminas/laminas-mail/issues/19 + */ + public function testHeadersToAndSubjectAreNotDuplicated() + { + $lineBreak = $this->isWindows() ? "\r\n" : "\n"; + + $message = new Message(); + $message + ->addTo('matthew@example.org') + ->addFrom('ralph@example.org') + ->setSubject('Greetings and Salutations!') + ->setBody("Sorry, I'm going to be late today!"); + + $this->transport->send($message); + + $this->assertEquals('matthew@example.org', $this->to); + $this->assertEquals('Greetings and Salutations!', $this->subject); + + $this->assertNotContains( + 'To: matthew@example.org, matthew@example.org' . $lineBreak, + $this->additional_headers + ); + $this->assertNotContains( + 'Subject: Greetings and Salutations!, Greetings and Salutations!' . $lineBreak, + $this->additional_headers + ); + } } From 47499254a49929a6a709072c7f00e94610dd9553 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 6 Aug 2020 03:55:05 +0700 Subject: [PATCH 2/3] update test assert to simulate scenario subject not duplicated Signed-off-by: Abdul Malik Ikhsan --- test/Transport/SendmailTest.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/Transport/SendmailTest.php b/test/Transport/SendmailTest.php index 3b740065..0244239e 100644 --- a/test/Transport/SendmailTest.php +++ b/test/Transport/SendmailTest.php @@ -281,13 +281,7 @@ public function testHeadersToAndSubjectAreNotDuplicated() $this->assertEquals('matthew@example.org', $this->to); $this->assertEquals('Greetings and Salutations!', $this->subject); - $this->assertNotContains( - 'To: matthew@example.org, matthew@example.org' . $lineBreak, - $this->additional_headers - ); - $this->assertNotContains( - 'Subject: Greetings and Salutations!, Greetings and Salutations!' . $lineBreak, - $this->additional_headers - ); + $this->assertNotRegExp('/^To: matthew\@example\.org$/m', $this->additional_headers); + $this->assertNotRegExp('/^Subject: Greetings and Salutations\!$/m', $this->additional_headers); } } From 461d35ae76b66e33df0c1614899c3c206bb9ab97 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 6 Aug 2020 19:53:43 +0700 Subject: [PATCH 3/3] Sendmail test clean up Signed-off-by: Abdul Malik Ikhsan --- test/Transport/SendmailTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/Transport/SendmailTest.php b/test/Transport/SendmailTest.php index 0244239e..111f8f3b 100644 --- a/test/Transport/SendmailTest.php +++ b/test/Transport/SendmailTest.php @@ -73,7 +73,7 @@ public function getMessage() return $message; } - public function isWindows() + private function isWindows() { return $this->operating_system === 'WIN'; } @@ -263,12 +263,10 @@ public function testDoNotAllowMessageWithoutToAndCcAndBccHeaders() } /** - * @see @see https://github.com/laminas/laminas-mail/issues/19 + * @see https://github.com/laminas/laminas-mail/issues/19 */ public function testHeadersToAndSubjectAreNotDuplicated() { - $lineBreak = $this->isWindows() ? "\r\n" : "\n"; - $message = new Message(); $message ->addTo('matthew@example.org') @@ -282,6 +280,6 @@ public function testHeadersToAndSubjectAreNotDuplicated() $this->assertEquals('Greetings and Salutations!', $this->subject); $this->assertNotRegExp('/^To: matthew\@example\.org$/m', $this->additional_headers); - $this->assertNotRegExp('/^Subject: Greetings and Salutations\!$/m', $this->additional_headers); + $this->assertNotRegExp('/^Subject: Greetings and Salutations!$/m', $this->additional_headers); } }