diff --git a/src/Protocol/Smtp.php b/src/Protocol/Smtp.php index 6e31b376..40c0434d 100644 --- a/src/Protocol/Smtp.php +++ b/src/Protocol/Smtp.php @@ -282,6 +282,11 @@ public function data($data) $this->_send('DATA'); $this->_expect(354, 120); // Timeout set for 2 minutes as per RFC 2821 4.5.3.2 + // Ensure newlines are CRLF (\r\n) + if (PHP_EOL === "\n") { + $data = str_replace("\n", "\r\n", str_replace("\r", '', $data)); + } + foreach (explode(self::EOL, $data) as $line) { if (strpos($line, '.') === 0) { // Escape lines prefixed with a '.' diff --git a/test/Protocol/SmtpTest.php b/test/Protocol/SmtpTest.php index 3bc3df2e..a870dc95 100644 --- a/test/Protocol/SmtpTest.php +++ b/test/Protocol/SmtpTest.php @@ -57,6 +57,34 @@ public function testSendMinimalMail() $this->assertEquals($expectedMessage, $this->connection->getLog()); } + public function testSendEscapedEmail() + { + $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("This is a test\n.") + ->addTo('zf-devteam@zend.com', 'ZF DevTeam') + ; + $expectedMessage = "EHLO localhost\r\n" + . "MAIL FROM:\r\n" + . "DATA\r\n" + . "Date: Sun, 10 Jun 2012 20:07:24 +0200\r\n" + . "Sender: Ralph Schindler \r\n" + . "To: ZF DevTeam \r\n" + . "\r\n" + . "This is a test\r\n" + . "..\r\n" + . ".\r\n"; + + $this->transport->send($message); + + $this->assertEquals($expectedMessage, $this->connection->getLog()); + + } + public function testDisconnectCallsQuit() { $this->connection->disconnect();