Skip to content
Snippets Groups Projects
Commit 6ca3110c authored by Fabian Galinski's avatar Fabian Galinski :pouting_cat:
Browse files

[FEATURE] Registration of the mail sending command controller

parent 8f74c61b
No related branches found
No related tags found
1 merge request!1Feature sg mail
<?php
namespace SGalinski\SgMail\Command;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Domain\Model\Mail;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
/**
* Command controller for the mailing feature
*/
class SendMailCommandController extends CommandController {
/**
* @var \SGalinski\SgMail\Domain\Repository\MailRepository
* @inject
*/
protected $mailRepository;
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
* @inject
*/
protected $persistenceManager;
/**
* Executes the sending of mails with a maximum of the given sentCount.
*
* @param int $sendCount
* @return void
*/
public function runSendMailsCommand($sendCount = 50) {
$mailsToSend = $this->mailRepository->findMailsToSend($sendCount);
foreach ($mailsToSend as $mailToSend) {
/** @var Mail $mailToSend */
$fromAddress = $mailToSend->getFromAddress();
$toAddress = $mailToSend->getToAddress();
$mailSubject = $mailToSend->getMailSubject();
$mailBody = $mailToSend->getMailBody();
$mailToSend->setSent(TRUE);
$this->mailRepository->update($mailToSend);
if (empty($fromAddress) || empty($toAddress) || empty($mailSubject)) {
continue;
}
$mailMessage = $this->objectManager->get(MailMessage::class);
$mailMessage->setFrom($fromAddress);
$mailMessage->setTo($toAddress);
$mailMessage->setSubject($mailSubject);
$mailMessage->setBody($mailBody, 'text/html');
$mailMessage->send();
}
// Important for command controllers that change data
$this->persistenceManager->persistAll();
}
}
?>
......@@ -26,11 +26,28 @@ namespace SGalinski\SgMail\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Extbase\Persistence\Generic\Query;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* Repository for the Mail object
*/
class MailRepository extends Repository {
/**
* Returns all mails that are still not sent ordered by priority.
*
* @param int $limit
* @return array|QueryResultInterface
*/
public function findMailsToSend($limit = NULL) {
$query = $this->createQuery();
if ($limit) {
$query->setLimit($limit);
}
$query->setOrderings(['priority' => Query::ORDER_DESCENDING]);
return $query->matching($query->equals('sent', FALSE))->execute();
}
}
......@@ -2,7 +2,9 @@
namespace SGalinski\SgMail\Service;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Repository\MailRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/***************************************************************
......@@ -99,11 +101,39 @@ class MailTemplateService {
$emailView->assignMultiple($content);
$emailBody = $emailView->render();
$mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
$mail->setFrom($fromAddress);
$mail->setTo($toAddress);
$mail->setSubject($subject);
$mail->setBody($emailBody, 'text/html');
$mail->send();
// @todo evtl. sollten hier noch die Parameter $priority und $pid mit gegeben werden.
self::addMailToMailQueue($fromAddress, $toAddress, $subject, $emailBody);
}
/**
* Adds a new mail to the mail queue. This will have the given parameters.
*
* @param string $fromAddress
* @param string $toAddress
* @param string $mailSubject
* @param string $mailBody
* @param int $priority
* @param int $pid
*/
public static function addMailToMailQueue(
$fromAddress, $toAddress, $mailSubject, $mailBody = '', $priority = Mail::PRIORITY_LOWEST, $pid = 0
) {
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
if ($pid <= 0 && isset($GLOBALS['TSFE'])) {
$pid = (int) $GLOBALS['TSFE']->id;
}
$mail = $objectManager->get(Mail::class);
$mail->setPid($pid);
$mail->setFromAddress($fromAddress);
$mail->setToAddress($toAddress);
$mail->setMailSubject($mailSubject);
$mail->setMailBody($mailBody);
$mail->setPriority($priority);
$mailRepository = $objectManager->get(MailRepository::class);
$mailRepository->add($mail);
}
}
......@@ -72,6 +72,10 @@ $description = 'E-Mail for confirmation of registration process';
'sg_mail', 'registration_confirmation', $templatePath, $description, $marker
);
// register command controllers
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] =
\SGalinski\SgMail\Command\SendMailCommandController::class;
if (TYPO3_MODE === 'BE') {
$tsPath = $extPath . 'Configuration/TypoScript/';
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.ts'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment