Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added MailTransport test class refs #2824

  • Loading branch information...
commit 9ac5cbed3669a0af6faa6e8130ce2994213e8177 1 parent 261b6b0
@lorenzo lorenzo authored
View
29 lib/Cake/Network/Email/MailTransport.php
@@ -41,14 +41,31 @@ public function send(CakeEmail $email) {
unset($headers['To']);
$headers = $this->_headersToString($headers, $eol);
$message = implode($eol, $email->message());
- if (ini_get('safe_mode') || !isset($this->_config['additionalParameters'])) {
- if (!@mail($to, $email->subject(), $message, $headers)) {
- throw new SocketException(__d('cake_dev', 'Could not send email.'));
- }
- } elseif (!@mail($to, $email->subject(), $message, $headers, $this->_config['additionalParameters'])) {
- throw new SocketException(__d('cake_dev', 'Could not send email.'));
+
+ $params = null;
+ if (!ini_get('safe_mode')) {
+ $params = isset($this->_config['additionalParameters']) ? $this->_config['additionalParameters'] : null;
}
+
+ $this->_mail($to, $email->subject(), $message, $headers, $params);
return array('headers' => $headers, 'message' => $message);
}
+/**
+ * Wraps internal function mail() and throws exception instead of errors if anything goes wrong
+ *
+ * @param string $to email's recipient
+ * @param string $subject email's subject
+ * @param string $message email's body
+ * @param string $headers email's custom headers
+ * @param string $params additional params for sending email
+ * @throws SocketException if mail could not be sent
+ * @return void
+ */
+ protected function _mail($to, $subject, $message, $headers, $params = null) {
+ if (!@mail($to, $subject, $message, $headers, $params)) {
+ throw new SocketException(__d('cake_dev', 'Could not send email.'));
+ }
+ }
+
}
View
84 lib/Cake/Test/Case/Network/Email/MailTransportTest.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * SmtpTransportTest file
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @package Cake.Test.Case.Network.Email
+ * @since CakePHP(tm) v 2.0.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+App::uses('CakeEmail', 'Network/Email');
+App::uses('AbstractTransport', 'Network/Email');
+App::uses('MailTransport', 'Network/Email');
+
+
+/**
+ * Test case
+ *
+ */
+class MailTransportTest extends CakeTestCase {
+
+/**
+ * Setup
+ *
+ * @return void
+ */
+ public function setUp() {
+ $this->MailTransport = $this->getMock('MailTransport', array('_mail'));
+ $this->MailTransport->config(array('additionalParameters' => '-f'));
+ }
+
+/**
+ * testSend method
+ *
+ * @return void
+ */
+ public function testSendData() {
+ $email = $this->getMock('CakeEmail', array('message'), array());
+ $email->from('noreply@cakephp.org', 'CakePHP Test');
+ $email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
+ $email->to('cake@cakephp.org', 'CakePHP');
+ $email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
+ $email->bcc('phpnut@cakephp.org');
+ $email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
+ $email->subject('Foø Bår Béz Foø Bår Béz Foø Bår Béz Foø Bår Béz');
+ $date = date(DATE_RFC2822);
+ $email->setHeaders(array('X-Mailer' => 'CakePHP Email', 'Date' => $date));
+ $email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
+
+ $data = "From: CakePHP Test <noreply@cakephp.org>" . PHP_EOL;
+ $data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>" . PHP_EOL;
+ $data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>" . PHP_EOL;
+ $data .= "Bcc: phpnut@cakephp.org" . PHP_EOL;
+ $data .= "X-Mailer: CakePHP Email" . PHP_EOL;
+ $data .= "Date: " . $date . PHP_EOL;
+ $data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>" . PHP_EOL;
+ $data .= "MIME-Version: 1.0" . PHP_EOL;
+ $data .= "Content-Type: text/plain; charset=UTF-8" . PHP_EOL;
+ $data .= "Content-Transfer-Encoding: 8bit";
+
+ $subject = '=?UTF-8?B?Rm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXogRm/DuCBCw6VyIELDqXog?=';
+ $subject .= "\r\n" . ' =?UTF-8?B?Rm/DuCBCw6VyIELDqXo=?=';
+ $this->MailTransport->expects($this->once())->method('_mail')
+ ->with(
+ 'CakePHP <cake@cakephp.org>',
+ $subject,
+ implode(PHP_EOL, array('First Line', 'Second Line', '.Third Line', '')),
+ $data,
+ '-f'
+ );
+
+ $this->MailTransport->send($email);
+ }
+
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.