Newer
Older
<?php
namespace SGalinski\SgMail\Command;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://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 SGalinski\SgMail\Service\PlaintextService;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
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
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
public function runSendMailsCommand($sendCount = 50) {
$mailsToSend = $this->mailRepository->findMailsToSend($sendCount);
foreach ($mailsToSend as $mailToSend) {
/** @var Mail $mailToSend */
$fromAddress = $mailToSend->getFromAddress();
$toAddresses = trim($mailToSend->getToAddress());
$addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE);
if (\count($addressesArray) > 1) {
$toAddresses = $addressesArray;
}
$ccAddresses = GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses(), TRUE);
$bccAddresses = GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses(), TRUE);
$mailSubject = $mailToSend->getMailSubject();
$mailBody = $mailToSend->getMailBody();
$mailToSend->setSendingTime(time());
$mailToSend->setLastSendingTime(time());
if (empty($fromAddress) || empty($toAddresses)) {
continue;
}
$this->mailRepository->update($mailToSend);
$mailMessage = $this->objectManager->get(MailMessage::class);
$mailMessage->setFrom($fromAddress, $mailToSend->getFromName());
$mailMessage->setTo($toAddresses);
$mailMessage->setSubject($mailSubject);
if (count($ccAddresses)) {
$mailMessage->setCc($ccAddresses);
}
if (count($bccAddresses)) {
$mailMessage->setBcc($bccAddresses);
}
$mailMessage->setBody($mailBody, 'text/html');
$plaintextService = GeneralUtility::makeInstance(PlaintextService::class);
$plaintextBody = $plaintextService->makePlain($mailBody);
$mailMessage->addPart($plaintextBody, 'text/plain');
$attachments = $mailToSend->getAttachments();
if ($attachments->count() > 0) {
foreach ($attachments as $attachment) {
/**
* @var FileReference $attachment
*/
$file = $attachment->getOriginalResource()->getOriginalFile();
$mailMessage->attach(
\Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType())
);
}
}
try {
$mailMessage->send();
} catch (\Exception $exception) {
// @TODO proper error handling is missing, save this into the database?
}
}
// Important for command controllers that change data
$this->persistenceManager->persistAll();
}
}