Commit 999a9753 authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[WIP] Sending mails from queue

parent a0ceec24
......@@ -24,8 +24,11 @@ namespace SGalinski\SgMail\Controller;
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Controller for the mail templating service module
......@@ -62,4 +65,23 @@ class QueueController extends ActionController {
$this->view->assign('queue', $queue->toArray());
$this->view->assign('mode', 'queue');
}
/**
* send or resend a mail in the queue
*
* @param int $uid
*/
public function sendMailAction($uid) {
/** @var Mail $mail */
$mail = $this->mailRepository->findByUid($uid)->getFirst();
$mailService = new MailTemplateService();
$mailService->sendMailFromQueue($uid);
$message = LocalizationUtility::translate('backend.success', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::OK);
$arguments = $this->request->getArguments();
$this->redirect('index', NULL, NULL, $arguments);
}
}
......@@ -70,4 +70,15 @@ class MailRepository extends AbstractRepository {
$query->setOrderings(['tstamp' => Query::ORDER_DESCENDING]);
return $query->matching($query->logicalAnd($constraintsAnd))->execute();
}
/**
* Find a mail in the queue by its uid
*
* @param int $uid
* @return array|QueryResultInterface
*/
public function findByUid($uid) {
$query = $this->createQuery();
return $query->matching($query->equals('uid', $uid))->execute();
}
}
......@@ -279,10 +279,10 @@ class MailTemplateService {
$this->setBccAddresses(explode(',', $template->getBcc()));
$this->setReplyToAddress($template->getReplyTo());
$this->setFromAddress($template->getFromMail(), $template->getFromName());
return true;
return TRUE;
}
return false;
return FALSE;
}
/**
......@@ -361,7 +361,9 @@ class MailTemplateService {
$emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $emailBody);
if ($this->ignoreMailQueue) {
$this->addMailToMailQueue($this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority, TRUE);
$this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority, TRUE
);
$this->mailMessage->setBody($emailBody, 'text/html');
$this->mailMessage->send();
} else {
......@@ -399,6 +401,26 @@ class MailTemplateService {
$this->persistenceManager->persistAll();
}
/**
* Send a Mail from the queue, identified by its id
*
* @param int $uid
*/
public function sendMailFromQueue($uid) {
$mailRepository = $this->objectManager->get(MailRepository::class);
/** @var Mail $mailToSend */
$mailToSend = $mailRepository->findByUid($uid)->getFirst();
if ($mailToSend) {
$this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html');
$this->mailMessage->setTo($mailToSend->getToAddress());
$this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName());
$this->mailMessage->setSubject($mailToSend->getMailSubject());
$this->mailMessage->send();
}
}
/**
* @param array $registerArray
* @return void
......
......@@ -16,14 +16,26 @@
<th><f:translate key="backend.fromAddress" /></th>
<th><f:translate key="backend.sent" /></th>
<th><f:translate key="backend.priority" /></th>
<th></th>
</tr>
</thead>
<f:for each="{queue}" as="mail">
<tr>
<td>{mail.toAddress}</td>
<td>{mail.fromAddress}</td>
<td>{mail.sent}</td>
<td>{mail.priority}</td>
<td>{mail.fromAddress}</td>
<td>
<f:if condition="{mail.sent} == '0'">
<f:then>
<f:link.action class="btn btn-primary" controller="Queue" action="sendMail" arguments="{uid: mail.uid}">Send Now</f:link.action>
</f:then>
<f:else>
<f:link.action class="btn btn-warning" controller="Queue" action="sendMail" arguments="{uid: mail.uid}">Send Again</f:link.action>
</f:else>
</f:if>
</td>
</tr>
</f:for>
</table>
......
......@@ -11,7 +11,7 @@ if (!defined('TYPO3_MODE')) {
'',
[
'Mail' => 'index, save, sendTestMail, empty, reset',
'Queue' => 'index',
'Queue' => 'index, sendMail',
],
[
'access' => 'admin,user,group',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment