[BUGFIX] Fix the unit tests to work with PHPUnit 3.6
[Packages/TYPO3.CMS.git] / t3lib / mail / class.t3lib_mail_mboxtransport.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Ernesto Baschny <ernst@cron-it.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
30 *
31 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
32 * for it to work out-of-the-box.
33 *
34 * $Id$
35 *
36 * @author Ernesto Baschny <ernst@cron-it.de>
37 * @package TYPO3
38 * @subpackage t3lib
39 */
40 class t3lib_mail_MboxTransport implements Swift_Transport {
41
42 /**
43 * @var string The file to write our mails into
44 */
45 private $debugFile;
46
47 /**
48 * Create a new MailTransport
49 * @param Swift_Transport_Log $log
50 */
51 public function __construct($debugFile) {
52 $this->debugFile = $debugFile;
53 }
54
55 /**
56 * Not used.
57 */
58 public function isStarted() {
59 return FALSE;
60 }
61
62 /**
63 * Not used.
64 */
65 public function start() {
66 }
67
68 /**
69 * Not used.
70 */
71 public function stop() {
72 }
73
74 /**
75 * Outputs the mail to a text file according to RFC 4155.
76 *
77 * @param Swift_Mime_Message $message The message to send
78 * @param string[] &$failedRecipients To collect failures by-reference, nothing will fail in our debugging case
79 * @return int
80 * @throws RuntimeException
81 */
82 public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
83 $message->generateId();
84
85 // Create a mbox-like header
86 $mboxFrom = $this->getReversePath($message);
87 $mboxDate = strftime('%c', $message->getDate());
88 $messageStr = sprintf('From %s %s', $mboxFrom, $mboxDate) . LF;
89
90 // Add the complete mail inclusive headers
91 $messageStr .= $message->toString();
92 $messageStr .= LF . LF;
93
94 $lockObject = t3lib_div::makeInstance('t3lib_lock', $this->debugFile, $GLOBALS['TYPO3_CONF_VARS']['SYS']['lockingMode']);
95 /** @var t3lib_lock $lockObject */
96 $lockObject->acquire();
97
98 // Write the mbox file
99 $file = @fopen($this->debugFile, 'a');
100 if (!$file) {
101 $lockObject->release();
102 throw new Exception(
103 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile),
104 1291064151
105 );
106 }
107
108 @fwrite($file, $messageStr);
109 @fclose($file);
110
111 t3lib_div::fixPermissions($this->debugFile);
112
113 $lockObject->release();
114
115 // Return every receipient as "delivered"
116 $count = (
117 count((array) $message->getTo())
118 + count((array) $message->getCc())
119 + count((array) $message->getBcc())
120 );
121 return $count;
122 }
123
124 /**
125 * Determine the best-use reverse path for this message
126 *
127 * @param Swift_Mime_Message $message
128 * @return mixed|null
129 */
130 private function getReversePath(Swift_Mime_Message $message) {
131 $return = $message->getReturnPath();
132 $sender = $message->getSender();
133 $from = $message->getFrom();
134 $path = NULL;
135 if (!empty($return)) {
136 $path = $return;
137 } elseif (!empty($sender)) {
138 $keys = array_keys($sender);
139 $path = array_shift($keys);
140 } elseif (!empty($from)) {
141 $keys = array_keys($from);
142 $path = array_shift($keys);
143 }
144 return $path;
145 }
146
147 /**
148 * Register a plugin in the Transport.
149 *
150 * @param Swift_Events_EventListener $plugin
151 */
152 public function registerPlugin(Swift_Events_EventListener $plugin) {
153 return TRUE;
154 }
155 }
156
157 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']) {
158 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']);
159 }
160
161 ?>