[BUGFIX] Autoloader Cache is not updated
[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 * @author Ernesto Baschny <ernst@cron-it.de>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_mail_MboxTransport implements Swift_Transport {
39
40 /**
41 * @var string The file to write our mails into
42 */
43 private $debugFile;
44
45 /**
46 * Create a new MailTransport
47 * @param Swift_Transport_Log $log
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 * Not used.
68 */
69 public function stop() {
70 }
71
72 /**
73 * Outputs the mail to a text file according to RFC 4155.
74 *
75 * @param Swift_Mime_Message $message The message to send
76 * @param string[] &$failedRecipients To collect failures by-reference, nothing will fail in our debugging case
77 * @return int
78 * @throws Exception
79 */
80 public function send(Swift_Mime_Message $message, &$failedRecipients = NULL) {
81 $message->generateId();
82
83 // Create a mbox-like header
84 $mboxFrom = $this->getReversePath($message);
85 $mboxDate = strftime('%c', $message->getDate());
86 $messageStr = sprintf('From %s %s', $mboxFrom, $mboxDate) . LF;
87
88 // Add the complete mail inclusive headers
89 $messageStr .= $message->toString();
90 $messageStr .= LF . LF;
91
92 // Write the mbox file
93 $file = @fopen($this->debugFile, 'a');
94 if (!$file) {
95 throw new RuntimeException(
96 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile),
97 1291064151
98 );
99 }
100
101 flock($file, LOCK_EX);
102 @fwrite($file, $messageStr);
103 flock($file, LOCK_UN);
104 @fclose($file);
105
106 t3lib_div::fixPermissions($this->debugFile);
107
108 // Return every receipient as "delivered"
109 $count = (
110 count((array) $message->getTo())
111 + count((array) $message->getCc())
112 + count((array) $message->getBcc())
113 );
114 return $count;
115 }
116
117 /**
118 * Determine the best-use reverse path for this message
119 *
120 * @param Swift_Mime_Message $message
121 * @return mixed|NULL
122 */
123 private function getReversePath(Swift_Mime_Message $message) {
124 $return = $message->getReturnPath();
125 $sender = $message->getSender();
126 $from = $message->getFrom();
127 $path = NULL;
128 if (!empty($return)) {
129 $path = $return;
130 } elseif (!empty($sender)) {
131 $keys = array_keys($sender);
132 $path = array_shift($keys);
133 } elseif (!empty($from)) {
134 $keys = array_keys($from);
135 $path = array_shift($keys);
136 }
137 return $path;
138 }
139
140 /**
141 * Register a plugin in the Transport.
142 *
143 * @param Swift_Events_EventListener $plugin
144 */
145 public function registerPlugin(Swift_Events_EventListener $plugin) {
146 return TRUE;
147 }
148 }
149
150 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']) {
151 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']);
152 }
153
154 ?>