[BUGFIX] Fix terms in Info > Page TSconfig
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / TestTask.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Scheduler\Example;
17
18 use Psr\Http\Message\ServerRequestInterface;
19 use Symfony\Component\Mime\Address;
20 use TYPO3\CMS\Core\Core\Environment;
21 use TYPO3\CMS\Core\Mail\FluidEmail;
22 use TYPO3\CMS\Core\Mail\Mailer;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Fluid\View\TemplatePaths;
25 use TYPO3\CMS\Scheduler\Task\AbstractTask;
26
27 /**
28 * Provides testing procedures
29 * @internal This class is an example is not considered part of the Public TYPO3 API.
30 */
31 class TestTask extends AbstractTask
32 {
33 /**
34 * An email address to be used during the process
35 *
36 * @var string $email
37 */
38 public $email;
39
40 /**
41 * Function executed from the Scheduler.
42 * Sends an email
43 *
44 * @return bool
45 */
46 public function execute()
47 {
48 if (!empty($this->email)) {
49 // If an email address is defined, send a message to it
50 $this->logger->info('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: Test email sent to "{email}"', ['email' => $this->email]);
51
52 $templateConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
53 $templateConfiguration['templateRootPaths'][20] = 'EXT:scheduler/Resources/Private/Templates/Email/';
54
55 if (Environment::isCli()) {
56 $calledBy = 'CLI module dispatcher';
57 $site = '-';
58 } else {
59 $calledBy = 'TYPO3 backend';
60 $site = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
61 }
62 $email = GeneralUtility::makeInstance(
63 FluidEmail::class,
64 GeneralUtility::makeInstance(TemplatePaths::class, $templateConfiguration)
65 );
66 $fromAddress = !empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])
67 ? $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']
68 : $this->email;
69 $email
70 ->to($this->email)
71 ->subject('SCHEDULER TEST-TASK')
72 ->from(new Address($fromAddress, 'SCHEDULER TEST-TASK'))
73 ->setTemplate('TestTask')
74 ->assignMultiple(
75 [
76 'data' => [
77 'uid' => $this->taskUid,
78 'site' => $site,
79 'calledBy' => $calledBy,
80 'tstamp' => time(),
81 'maxLifetime' => $this->scheduler->extConf['maxLifetime'],
82 ],
83 'exec' => $this->getExecution()
84 ]
85 );
86
87 if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
88 $email->setRequest($GLOBALS['TYPO3_REQUEST']);
89 }
90 GeneralUtility::makeInstance(Mailer::class)->send($email);
91 return true;
92 }
93 // No email defined, just log the task
94 $this->logger->warning('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: No email address given');
95
96 return false;
97 }
98
99 /**
100 * This method returns the destination mail address as additional information
101 *
102 * @return string Information to display
103 */
104 public function getAdditionalInformation()
105 {
106 return $this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email') . ': ' . $this->email;
107 }
108 }