897d7508dc0277cb08b2bedb69db6c25369c3927
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / MboxTransport.php
1 <?php
2 namespace TYPO3\CMS\Core\Mail;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Locking\LockFactory;
19
20 /**
21 * Adapter for Swift_Mailer to be used by TYPO3 extensions.
22 *
23 * This will use the setting in TYPO3_CONF_VARS to choose the correct transport
24 * for it to work out-of-the-box.
25 *
26 * @author Ernesto Baschny <ernst@cron-it.de>
27 */
28 class MboxTransport implements \Swift_Transport {
29
30 /**
31 * @var string The file to write our mails into
32 */
33 private $debugFile;
34
35 /**
36 * Create a new MailTransport
37 *
38 * @param string $debugFile
39 */
40 public function __construct($debugFile) {
41 $this->debugFile = $debugFile;
42 }
43
44 /**
45 * Not used.
46 */
47 public function isStarted() {
48 return FALSE;
49 }
50
51 /**
52 * Not used.
53 */
54 public function start() {
55
56 }
57
58 /**
59 * Not used.
60 */
61 public function stop() {
62
63 }
64
65 /**
66 * Outputs the mail to a text file according to RFC 4155.
67 *
68 * @param \Swift_Mime_Message $message The message to send
69 * @param string[] &$failedRecipients To collect failures by-reference, nothing will fail in our debugging case
70 * @return int
71 * @throws \RuntimeException
72 */
73 public function send(\Swift_Mime_Message $message, &$failedRecipients = NULL) {
74 $message->generateId();
75 // Create a mbox-like header
76 $mboxFrom = $this->getReversePath($message);
77 $mboxDate = strftime('%c', $message->getDate());
78 $messageStr = sprintf('From %s %s', $mboxFrom, $mboxDate) . LF;
79 // Add the complete mail inclusive headers
80 $messageStr .= $message->toString();
81 $messageStr .= LF . LF;
82 $lockFactory = GeneralUtility::makeInstance(LockFactory::class);
83 $lockObject = $lockFactory->createLocker($this->debugFile);
84 $lockObject->acquire();
85 // Write the mbox file
86 $file = @fopen($this->debugFile, 'a');
87 if (!$file) {
88 $lockObject->release();
89 throw new \RuntimeException(sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile), 1291064151);
90 }
91 @fwrite($file, $messageStr);
92 @fclose($file);
93 GeneralUtility::fixPermissions($this->debugFile);
94 $lockObject->release();
95 // Return every receipient as "delivered"
96 $count = count((array)$message->getTo()) + count((array)$message->getCc()) + count((array)$message->getBcc());
97 return $count;
98 }
99
100 /**
101 * Determine the best-use reverse path for this message
102 *
103 * @param \Swift_Mime_Message $message
104 * @return mixed|NULL
105 */
106 private function getReversePath(\Swift_Mime_Message $message) {
107 $return = $message->getReturnPath();
108 $sender = $message->getSender();
109 $from = $message->getFrom();
110 $path = NULL;
111 if (!empty($return)) {
112 $path = $return;
113 } elseif (!empty($sender)) {
114 $keys = array_keys($sender);
115 $path = array_shift($keys);
116 } elseif (!empty($from)) {
117 $keys = array_keys($from);
118 $path = array_shift($keys);
119 }
120 return $path;
121 }
122
123 /**
124 * Register a plugin in the Transport.
125 *
126 * @param \Swift_Events_EventListener $plugin
127 * @return bool
128 */
129 public function registerPlugin(\Swift_Events_EventListener $plugin) {
130 return TRUE;
131 }
132
133 }