TYPO3 CMS  TYPO3_8-7
MboxTransport.php
Go to the documentation of this file.
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 
19 
26 class MboxTransport implements \Swift_Transport
27 {
31  private $debugFile;
32 
38  public function __construct($debugFile)
39  {
40  $this->debugFile = $debugFile;
41  }
42 
46  public function isStarted()
47  {
48  return false;
49  }
50 
54  public function start()
55  {
56  }
57 
61  public function stop()
62  {
63  }
64 
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 
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 
131  public function registerPlugin(\Swift_Events_EventListener $plugin)
132  {
133  return true;
134  }
135 }
registerPlugin(\Swift_Events_EventListener $plugin)
static makeInstance($className,... $constructorArguments)
static fixPermissions($path, $recursive=false)
getReversePath(\Swift_Mime_Message $message)
send(\Swift_Mime_Message $message, &$failedRecipients=null)