0d3d025b8e9303b08b081d3ce756c0703c46a367
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Example / TestTask.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Example;
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
19 /**
20 * Provides testing procedures
21 */
22 class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask
23 {
24 /**
25 * An email address to be used during the process
26 *
27 * @var string $email
28 */
29 public $email;
30
31 /**
32 * Function executed from the Scheduler.
33 * Sends an email
34 *
35 * @return bool
36 */
37 public function execute()
38 {
39 $success = false;
40 if (!empty($this->email)) {
41 // If an email address is defined, send a message to it
42 $this->logger->info('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: Test email sent to "' . $this->email . '"');
43 // Get execution information
44 $exec = $this->getExecution();
45 // Get call method
46 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
47 $calledBy = 'CLI module dispatcher';
48 $site = '-';
49 } else {
50 $calledBy = 'TYPO3 backend';
51 $site = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
52 }
53 $start = $exec->getStart();
54 $end = $exec->getEnd();
55 $interval = $exec->getInterval();
56 $multiple = $exec->getMultiple();
57 $cronCmd = $exec->getCronCmd();
58 $mailBody = 'SCHEDULER TEST-TASK' . LF . '- - - - - - - - - - - - - - - -' . LF . 'UID: ' . $this->taskUid . LF . 'Sitename: ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . LF . 'Site: ' . $site . LF . 'Called by: ' . $calledBy . LF . 'tstamp: ' . date('Y-m-d H:i:s') . ' [' . time() . ']' . LF . 'maxLifetime: ' . $this->scheduler->extConf['maxLifetime'] . LF . 'start: ' . date('Y-m-d H:i:s', $start) . ' [' . $start . ']' . LF . 'end: ' . (empty($end) ? '-' : date('Y-m-d H:i:s', $end) . ' [' . $end . ']') . LF . 'interval: ' . $interval . LF . 'multiple: ' . ($multiple ? 'yes' : 'no') . LF . 'cronCmd: ' . ($cronCmd ? $cronCmd : 'not used');
59 // Prepare mailer and send the mail
60 try {
61 /** @var $mailer \TYPO3\CMS\Core\Mail\MailMessage */
62 $mailer = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
63 $mailer->setFrom([$this->email => 'SCHEDULER TEST-TASK']);
64 $mailer->setReplyTo([$this->email => 'SCHEDULER TEST-TASK']);
65 $mailer->setSubject('SCHEDULER TEST-TASK');
66 $mailer->setBody($mailBody);
67 $mailer->setTo($this->email);
68 $mailsSend = $mailer->send();
69 $success = $mailsSend > 0;
70 } catch (\Exception $e) {
71 throw new \TYPO3\CMS\Core\Exception($e->getMessage(), 1476048416);
72 }
73 } else {
74 // No email defined, just log the task
75 $this->logger->warning('[TYPO3\\CMS\\Scheduler\\Example\\TestTask]: No email address given');
76 }
77 return $success;
78 }
79
80 /**
81 * This method returns the destination mail address as additional information
82 *
83 * @return string Information to display
84 */
85 public function getAdditionalInformation()
86 {
87 return $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.email') . ': ' . $this->email;
88 }
89 }