[BUGFIX] PublicURL calculation for storage 0 is wrong
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / MboxTransport.php
1 <?php
2 namespace TYPO3\CMS\Core\Mail;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Ernesto Baschny <ernst@cron-it.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
31 *
32 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
33 * for it to work out-of-the-box.
34 *
35 * @author Ernesto Baschny <ernst@cron-it.de>
36 */
37 class MboxTransport implements \Swift_Transport {
38
39 /**
40 * @var string The file to write our mails into
41 */
42 private $debugFile;
43
44 /**
45 * Create a new MailTransport
46 *
47 * @param string $debugFile
48 */
49 public function __construct($debugFile) {
50 $this->debugFile = $debugFile;
51 }
52
53 /**
54 * Not used.
55 */
56 public function isStarted() {
57 return FALSE;
58 }
59
60 /**
61 * Not used.
62 */
63 public function start() {
64
65 }
66
67 /**
68 * Not used.
69 */
70 public function stop() {
71
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 // Create a mbox-like header
85 $mboxFrom = $this->getReversePath($message);
86 $mboxDate = strftime('%c', $message->getDate());
87 $messageStr = sprintf('From %s %s', $mboxFrom, $mboxDate) . LF;
88 // Add the complete mail inclusive headers
89 $messageStr .= $message->toString();
90 $messageStr .= LF . LF;
91 $lockObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Locking\\Locker', $this->debugFile, $GLOBALS['TYPO3_CONF_VARS']['SYS']['lockingMode']);
92 /** @var \TYPO3\CMS\Core\Locking\Locker $lockObject */
93 $lockObject->acquire();
94 // Write the mbox file
95 $file = @fopen($this->debugFile, 'a');
96 if (!$file) {
97 $lockObject->release();
98 throw new \RuntimeException(sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile), 1291064151);
99 }
100 @fwrite($file, $messageStr);
101 @fclose($file);
102 \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($this->debugFile);
103 $lockObject->release();
104 // Return every receipient as "delivered"
105 $count = count((array) $message->getTo()) + count((array) $message->getCc()) + count((array) $message->getBcc());
106 return $count;
107 }
108
109 /**
110 * Determine the best-use reverse path for this message
111 *
112 * @param Swift_Mime_Message $message
113 * @return mixed|NULL
114 */
115 private function getReversePath(\Swift_Mime_Message $message) {
116 $return = $message->getReturnPath();
117 $sender = $message->getSender();
118 $from = $message->getFrom();
119 $path = NULL;
120 if (!empty($return)) {
121 $path = $return;
122 } elseif (!empty($sender)) {
123 $keys = array_keys($sender);
124 $path = array_shift($keys);
125 } elseif (!empty($from)) {
126 $keys = array_keys($from);
127 $path = array_shift($keys);
128 }
129 return $path;
130 }
131
132 /**
133 * Register a plugin in the Transport.
134 *
135 * @param Swift_Events_EventListener $plugin
136 */
137 public function registerPlugin(\Swift_Events_EventListener $plugin) {
138 return TRUE;
139 }
140
141 }