612fd1bd1d73be2e450cf5879bbd489c849d0498
[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 $lockObject = t3lib_div::makeInstance('t3lib_lock', $this->debugFile, $GLOBALS['TYPO3_CONF_VARS']['SYS']['lockingMode']);
93 /** @var t3lib_lock $lockObject */
94 $lockObject->acquire();
95
96 // Write the mbox file
97 $file = @fopen($this->debugFile, 'a');
98 if (!$file) {
99 $lockObject->release();
100 throw new RuntimeException(
101 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile),
102 1291064151
103 );
104 }
105
106 @fwrite($file, $messageStr);
107 @fclose($file);
108
109 t3lib_div::fixPermissions($this->debugFile);
110
111 $lockObject->release();
112
113 // Return every receipient as "delivered"
114 $count = (
115 count((array) $message->getTo())
116 + count((array) $message->getCc())
117 + count((array) $message->getBcc())
118 );
119 return $count;
120 }
121
122 /**
123 * Determine the best-use reverse path for this message
124 *
125 * @param Swift_Mime_Message $message
126 * @return mixed|NULL
127 */
128 private function getReversePath(Swift_Mime_Message $message) {
129 $return = $message->getReturnPath();
130 $sender = $message->getSender();
131 $from = $message->getFrom();
132 $path = NULL;
133 if (!empty($return)) {
134 $path = $return;
135 } elseif (!empty($sender)) {
136 $keys = array_keys($sender);
137 $path = array_shift($keys);
138 } elseif (!empty($from)) {
139 $keys = array_keys($from);
140 $path = array_shift($keys);
141 }
142 return $path;
143 }
144
145 /**
146 * Register a plugin in the Transport.
147 *
148 * @param Swift_Events_EventListener $plugin
149 */
150 public function registerPlugin(Swift_Events_EventListener $plugin) {
151 return TRUE;
152 }
153 }
154
155 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']) {
156 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']);
157 }
158
159 ?>