ab2b8f6925490aa97f3d7ac03f205786528805dc
[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\Locking\LockFactory;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 class MboxTransport implements \Swift_Transport
27 {
28 /**
29 * @var string The file to write our mails into
30 */
31 private $debugFile;
32
33 /**
34 * Create a new MailTransport
35 *
36 * @param string $debugFile
37 */
38 public function __construct($debugFile)
39 {
40 $this->debugFile = $debugFile;
41 }
42
43 /**
44 * Not used.
45 */
46 public function isStarted()
47 {
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 {
75 $message->generateId();
76 // Create a mbox-like header
77 $mboxFrom = $this->getReversePath($message);
78 $mboxDate = strftime('%c', $message->getDate());
79 $messageStr = sprintf('From %s %s', $mboxFrom, $mboxDate) . LF;
80 // Add the complete mail inclusive headers
81 $messageStr .= $message->toString();
82 $messageStr .= LF . LF;
83 $lockFactory = GeneralUtility::makeInstance(LockFactory::class);
84 $lockObject = $lockFactory->createLocker('mbox');
85 $lockObject->acquire();
86 // Write the mbox file
87 $file = @fopen($this->debugFile, 'a');
88 if (!$file) {
89 $lockObject->release();
90 throw new \RuntimeException(sprintf('Could not write to file "%s" when sending an email to debug transport', $this->debugFile), 1291064151);
91 }
92 @fwrite($file, $messageStr);
93 @fclose($file);
94 GeneralUtility::fixPermissions($this->debugFile);
95 $lockObject->release();
96 // Return every recipient as "delivered"
97 $count = count((array)$message->getTo()) + count((array)$message->getCc()) + count((array)$message->getBcc());
98 return $count;
99 }
100
101 /**
102 * Determine the best-use reverse path for this message
103 *
104 * @param \Swift_Mime_Message $message
105 * @return mixed|NULL
106 */
107 private function getReversePath(\Swift_Mime_Message $message)
108 {
109 $return = $message->getReturnPath();
110 $sender = $message->getSender();
111 $from = $message->getFrom();
112 $path = null;
113 if (!empty($return)) {
114 $path = $return;
115 } elseif (!empty($sender)) {
116 $keys = array_keys($sender);
117 $path = array_shift($keys);
118 } elseif (!empty($from)) {
119 $keys = array_keys($from);
120 $path = array_shift($keys);
121 }
122 return $path;
123 }
124
125 /**
126 * Register a plugin in the Transport.
127 *
128 * @param \Swift_Events_EventListener $plugin
129 * @return bool
130 */
131 public function registerPlugin(\Swift_Events_EventListener $plugin)
132 {
133 return true;
134 }
135 }