diff --git a/CHANGELOG.md b/CHANGELOG.md index 7025194b..ab519460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,10 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#93](https://github.com/zendframework/zend-mail/pull/93) fixes a situation + whereby `getSender()` was unintentionally creating a blank `Sender` header, + instead of returning `null` if none exists, fixing an issue in the SMTP + transport. ## 2.7.2 - 2016-12-19 diff --git a/src/Message.php b/src/Message.php index a1ed1243..55e14bb1 100644 --- a/src/Message.php +++ b/src/Message.php @@ -314,6 +314,10 @@ public function setSender($emailOrAddress, $name = null) */ public function getSender() { + $headers = $this->getHeaders(); + if (! $headers->has('sender')) { + return null; + } $header = $this->getHeaderByName('sender', __NAMESPACE__ . '\Header\Sender'); return $header->getAddress(); } diff --git a/test/MessageTest.php b/test/MessageTest.php index f3492e7d..fe858f1e 100644 --- a/test/MessageTest.php +++ b/test/MessageTest.php @@ -158,6 +158,13 @@ public function testSenderIsNullByDefault() $this->assertNull($this->message->getSender()); } + public function testNullSenderDoesNotCreateHeader() + { + $sender = $this->message->getSender(); + $headers = $this->message->getHeaders(); + $this->assertFalse($headers->has('sender')); + } + public function testSettingSenderCreatesAddressObject() { $this->message->setSender('zf-devteam@example.com'); diff --git a/test/Transport/SmtpTest.php b/test/Transport/SmtpTest.php index 90ddc00e..7978057e 100644 --- a/test/Transport/SmtpTest.php +++ b/test/Transport/SmtpTest.php @@ -37,19 +37,21 @@ public function setUp() public function getMessage() { $message = new Message(); - $message->addTo('zf-devteam@zend.com', 'ZF DevTeam') - ->addCc('matthew@zend.com') - ->addBcc('zf-crteam@lists.zend.com', 'CR-Team, ZF Project') - ->addFrom([ - 'zf-devteam@zend.com', - 'matthew@zend.com' => 'Matthew', - ]) - ->setSender('ralph.schindler@zend.com', 'Ralph Schindler') - ->setSubject('Testing Zend\Mail\Transport\Sendmail') - ->setBody('This is only a test.'); + $message->addTo('zf-devteam@zend.com', 'ZF DevTeam'); + $message->addCc('matthew@zend.com'); + $message->addBcc('zf-crteam@lists.zend.com', 'CR-Team, ZF Project'); + $message->addFrom([ + 'zf-devteam@zend.com', + 'matthew@zend.com' => 'Matthew', + ]); + $message->setSender('ralph.schindler@zend.com', 'Ralph Schindler'); + $message->setSubject('Testing Zend\Mail\Transport\Sendmail'); + $message->setBody('This is only a test.'); + $message->getHeaders()->addHeaders([ 'X-Foo-Bar' => 'Matthew', ]); + return $message; } @@ -135,18 +137,40 @@ public function testSendMinimalMail() { $headers = new Headers(); $headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200'); + + $message = new Message(); + $message->setHeaders($headers); + $message->setSender('ralph.schindler@zend.com', 'Ralph Schindler'); + $message->setBody('testSendMailWithoutMinimalHeaders'); + $message->addTo('zf-devteam@zend.com', 'ZF DevTeam'); + + $expectedMessage = "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n" + . "Sender: Ralph Schindler \r\n" + . "To: ZF DevTeam \r\n" + . "\r\n" + . "testSendMailWithoutMinimalHeaders"; + + $this->transport->send($message); + + $this->assertContains($expectedMessage, $this->connection->getLog()); + } + + public function testSendMinimalMailWithoutSender() + { + $headers = new Headers(); + $headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200'); + $message = new Message(); - $message - ->setHeaders($headers) - ->setSender('ralph.schindler@zend.com', 'Ralph Schindler') - ->setBody('testSendMailWithoutMinimalHeaders') - ->addTo('zf-devteam@zend.com', 'ZF DevTeam') - ; + $message->setHeaders($headers); + $message->setFrom('ralph.schindler@zend.com', 'Ralph Schindler'); + $message->setBody('testSendMinimalMailWithoutSender'); + $message->addTo('zf-devteam@zend.com', 'ZF DevTeam'); + $expectedMessage = "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n" - . "Sender: Ralph Schindler \r\n" - . "To: ZF DevTeam \r\n" - . "\r\n" - . "testSendMailWithoutMinimalHeaders"; + . "From: Ralph Schindler \r\n" + . "To: ZF DevTeam \r\n" + . "\r\n" + . "testSendMinimalMailWithoutSender"; $this->transport->send($message);