2 /***************************************************************
5 * (c) 2010 Ernesto Baschny <ernst@cron-it.de>
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
31 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
32 * for it to work out-of-the-box.
36 * @author Ernesto Baschny <ernst@cron-it.de>
40 class t3lib_mail_MboxTransport
implements Swift_Transport
{
43 * @var string The file to write our mails into
48 * Create a new MailTransport
49 * @param Swift_Transport_Log $log
51 public function __construct($debugFile) {
52 $this->debugFile
= $debugFile;
58 public function isStarted() {
65 public function start() {
71 public function stop() {
75 * Outputs the mail to a text file according to RFC 4155.
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
82 public function send(Swift_Mime_Message
$message, &$failedRecipients = null) {
83 $message->generateId();
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
;
90 // Add the complete mail inclusive headers
91 $messageStr .= $message->toString();
92 $messageStr .= LF
. LF
;
94 // Write the mbox file
95 $file = @fopen
($this->debugFile
, 'a');
98 sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile
),
103 flock($file, LOCK_EX
);
104 @fwrite
($file, $messageStr);
105 flock($file, LOCK_UN
);
108 t3lib_div
::fixPermissions($this->debugFile
);
110 // Return every receipient as "delivered"
112 count((array) $message->getTo())
113 +
count((array) $message->getCc())
114 +
count((array) $message->getBcc())
120 * Determine the best-use reverse path for this message
122 * @param Swift_Mime_Message $message
125 private function getReversePath(Swift_Mime_Message
$message) {
126 $return = $message->getReturnPath();
127 $sender = $message->getSender();
128 $from = $message->getFrom();
130 if (!empty($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);
143 * Register a plugin in the Transport.
145 * @param Swift_Events_EventListener $plugin
147 public function registerPlugin(Swift_Events_EventListener
$plugin) {
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']);