From 796e4b45dd092c7eb1a0ab128280578523191ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Gon=C3=A7alves?= Date: Wed, 12 Jun 2013 11:31:49 +0200 Subject: [PATCH] Using Return-Path email address in MAIL FROM instead of a header in SmtpTransport. The Return-Path header should not be present on the SMTP transaction, this header is set by the time of final delivery. Quote RFC2821: > When the delivery SMTP server makes the "final delivery" of a message, it inserts a return-path line at the beginning of the mail data. This use of return-path is required; mail systems MUST support it. The return-path line preserves the information in the from the MAIL command. --- lib/Cake/Network/Email/SmtpTransport.php | 7 ++++-- .../Case/Network/Email/SmtpTransportTest.php | 23 ++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Network/Email/SmtpTransport.php b/lib/Cake/Network/Email/SmtpTransport.php index 72a669c10..971a5cefd 100644 --- a/lib/Cake/Network/Email/SmtpTransport.php +++ b/lib/Cake/Network/Email/SmtpTransport.php @@ -161,7 +161,10 @@ class SmtpTransport extends AbstractTransport { * @throws SocketException */ protected function _sendRcpt() { - $from = $this->_cakeEmail->from(); + $from = $this->_cakeEmail->returnPath(); + if (empty($from)) { + $from = $this->_cakeEmail->from(); + } $this->_smtpSend('MAIL FROM:<' . key($from) . '>'); $to = $this->_cakeEmail->to(); @@ -182,7 +185,7 @@ class SmtpTransport extends AbstractTransport { protected function _sendData() { $this->_smtpSend('DATA', '354'); - $headers = $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'subject')); + $headers = $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'to', 'cc', 'subject')); $headers = $this->_headersToString($headers); $lines = $this->_cakeEmail->message(); $messages = array(); diff --git a/lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php b/lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php index 7418afb79..ab4e1dbca 100644 --- a/lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php +++ b/lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php @@ -270,6 +270,28 @@ class SmtpTransportTest extends CakeTestCase { $this->SmtpTransport->sendRcpt(); } +/** + * testRcptWithReturnPath method + * + * @return void + */ + public function testRcptWithReturnPath() { + $email = new CakeEmail(); + $email->from('noreply@cakephp.org', 'CakePHP Test'); + $email->to('cake@cakephp.org', 'CakePHP'); + $email->returnPath('pleasereply@cakephp.org', 'CakePHP Return'); + + $this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:\r\n"); + $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false)); + $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n")); + $this->socket->expects($this->at(3))->method('write')->with("RCPT TO:\r\n"); + $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false)); + $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n")); + + $this->SmtpTransport->setCakeEmail($email); + $this->SmtpTransport->sendRcpt(); + } + /** * testSendData method * @@ -290,7 +312,6 @@ class SmtpTransportTest extends CakeTestCase { $email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', ''))); $data = "From: CakePHP Test \r\n"; - $data .= "Return-Path: CakePHP Return \r\n"; $data .= "To: CakePHP \r\n"; $data .= "Cc: Mark Story , Juan Basso \r\n"; $data .= "X-Mailer: CakePHP Email\r\n";