Added Feature #16610: Provide a SwiftMailer transport that only saves send mails...
[Packages/TYPO3.CMS.git] / t3lib / mail / class.t3lib_mail_mboxtransport.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 Exception
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 // Write the mbox file
95 $file = @fopen($this->debugFile, 'a');
96 if (!$file) {
97 throw new Exception(
98 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile),
99 1291064151
100 );
101 }
102
103 flock($file, LOCK_EX);
104 @fwrite($file, $messageStr);
105 flock($file, LOCK_UN);
106 @fclose($file);
107
108 t3lib_div::fixPermissions($this->debugFile);
109
110 // Return every receipient as "delivered"
111 $count = (
112 count((array) $message->getTo())
113 + count((array) $message->getCc())
114 + count((array) $message->getBcc())
115 );
116 return $count;
117 }
118
119 /**
120 * Determine the best-use reverse path for this message
121 *
122 * @param Swift_Mime_Message $message
123 * @return mixed|null
124 */
125 private function getReversePath(Swift_Mime_Message $message) {
126 $return = $message->getReturnPath();
127 $sender = $message->getSender();
128 $from = $message->getFrom();
129 $path = NULL;
130 if (!empty($return)) {
131 $path = $return;
132 } elseif (!empty($sender)) {
133 $keys = array_keys($sender);
134 $path = array_shift($keys);
135 } elseif (!empty($from)) {
136 $keys = array_keys($from);
137 $path = array_shift($keys);
138 }
139 return $path;
140 }
141
142 /**
143 * Register a plugin in the Transport.
144 *
145 * @param Swift_Events_EventListener $plugin
146 */
147 public function registerPlugin(Swift_Events_EventListener $plugin) {
148 return TRUE;
149 }
150 }
151
152 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']) {
153 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mboxtransport.php']);
154 }
155
156 ?>