Commit c632dcf7 authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[FEATURE] Add attachments to mail queue

Enable the mail queue to store attachments and send them later
parent b14d4099
......@@ -30,6 +30,7 @@ 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;
/**
......@@ -92,6 +93,18 @@ class SendMailCommandController extends CommandController {
$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) {
......
......@@ -26,7 +26,9 @@ namespace SGalinski\SgMail\Domain\Model;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
/**
* Mail domain model
......@@ -114,6 +116,25 @@ class Mail extends AbstractEntity {
*/
protected $blacklisted = FALSE;
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
*/
protected $attachments;
/**
* Mail constructor.
*/
public function __construct() {
$this->initializeObject();
}
/**
* Initialize the object
*/
public function initializeObject() {
$this->attachments = new ObjectStorage();
}
/**
* @return string
*/
......@@ -328,4 +349,27 @@ class Mail extends AbstractEntity {
public function setBlacklisted($blacklisted) {
$this->blacklisted = (bool) $blacklisted;
}
/**
* @return ObjectStorage
*/
public function getAttachments() {
return $this->attachments;
}
/**
* @param ObjectStorage $attachments
*/
public function setAttachments(ObjectStorage $attachments) {
$this->attachments = $attachments;
}
/**
* Add an attachment
*
* @param FileReference $attachment
*/
public function addAttachment(FileReference $attachment) {
$this->attachments->attach($attachment);
}
}
......@@ -36,8 +36,10 @@ use Swift_OutputByteStream;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
......@@ -51,6 +53,7 @@ class MailTemplateService {
const MARKER_TYPE_STRING = 'String';
const MARKER_TYPE_ARRAY = 'Array';
const MARKER_TYPE_OBJECT = 'Object';
const MARKER_TYPE_FILE = 'File';
const DEFAULT_LANGUAGE = 'default';
const DEFAULT_TEMPLATE_PATH = 'Resources/Private/Templates/SgMail/';
const CACHE_NAME = 'sg_mail_registerArrayCache';
......@@ -338,9 +341,13 @@ class MailTemplateService {
self::getRegisterArray()[$this->extensionKey][$this->templateName]['subject'][$this->language]
);
}
$emailView->setTemplateSource($subject);
$subject = $emailView->render();
if ($subject === NULL && $this->subject !== NULL) {
$subject = $this->subject;
}
if ($subject !== NULL) {
$emailView->setTemplateSource($subject);
$subject = $emailView->render();
}
$emailView->setTemplateSource($defaultTemplateContent);
}
......@@ -427,6 +434,11 @@ class MailTemplateService {
$mail->setSendingTime($sendingTime);
$mail->setLastSendingTime($lastSendingTime);
$mail->setReplyTo($this->replyToAddress);
foreach ($this->markers as $marker) {
if ($marker instanceof FileReference) {
$mail->addAttachment($marker);
}
}
$mailRepository = $this->objectManager->get(MailRepository::class);
$mailRepository->add($mail);
......@@ -465,7 +477,18 @@ class MailTemplateService {
if ($mailToSend->getReplyTo()) {
$this->mailMessage->setReplyTo($mailToSend->getReplyTo());
}
$attachments = $mailToSend->getAttachments();
if ($attachments->count() > 0) {
foreach ($attachments as $attachment) {
/**
* @var FileReference $attachment
*/
$file = $attachment->getOriginalResource()->getOriginalFile();
$this->mailMessage->attach(
\Swift_Attachment::newInstance($file->getContents(), $file->getName(), $file->getMimeType())
);
}
}
$dateTime = new DateTime();
if ((int)$mailToSend->getSendingTime() === 0) {
$mailToSend->setSendingTime($dateTime->getTimestamp());
......@@ -609,6 +632,16 @@ class MailTemplateService {
return $this;
}
/**
* Add a file resource as attachment
*
* @param FileInterface $file
* @return MailTemplateService
*/
public function addFileResourceAttachment(FileInterface $file) {
return $this->addAttachment($file->getContents(), $file->getName(), $file->getMimeType());
}
/**
* @return MailMessage
*/
......@@ -666,6 +699,9 @@ class MailTemplateService {
case self::MARKER_TYPE_OBJECT :
LocalizationUtility::translate('backend.marker.type.object', 'sg_mail');
break;
case self::MARKER_TYPE_FILE:
LocalizationUtility::translate('backend.marker.type.file', 'sg_mail');
break;
default:
LocalizationUtility::translate('backend.marker.type.mixed', 'sg_mail');
}
......
......@@ -46,7 +46,7 @@ return [
],
'types' => [
'1' => [
'showitem' => 'hidden;;1, blacklisted, priority, to_address, from_address, mail_subject, mail_body, from_name, bcc_addresses, cc_addresses, extension_key, template_name, sending_time, last_sending_time, language'
'showitem' => 'hidden;;1, blacklisted, priority, to_address, from_address, mail_subject, mail_body, from_name, bcc_addresses, cc_addresses, extension_key, template_name, sending_time, last_sending_time, language, attachments'
],
],
'columns' => [
......@@ -184,6 +184,18 @@ return [
'type' => 'input',
'eval' => 'required, trim'
],
],
'attachments' => [
'exclude' => TRUE,
'label' => 'Attachments',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'attachments',
[
'appearance' => [
'collapseAll' => TRUE
],
'maxitems' => 9999
])
]
]
];
......@@ -189,6 +189,10 @@
<source>String</source>
<target>Zeichenkette</target>
</trans-unit>
<trans-unit id="backend.marker.type.file" approved="yes">
<source>File</source>
<target>Datei</target>
</trans-unit>
<trans-unit id="bachend.markerUsageInfo">
<source>Usage information</source>
<target>Information zur Benutzung</target>
......
......@@ -144,6 +144,9 @@
<trans-unit id="backend.marker.type.string">
<source>String</source>
</trans-unit>
<trans-unit id="backend.marker.type.file">
<source>File</source>
</trans-unit>
<trans-unit id="bachend.markerUsageInfo">
<source>Usage information</source>
</trans-unit>
......
......@@ -17,6 +17,7 @@ CREATE TABLE tx_sgmail_domain_model_mail (
last_sending_time int(11) unsigned DEFAULT '0' NOT NULL,
language varchar(255) DEFAULT '' NOT NULL,
blacklisted tinyint(4) unsigned DEFAULT '0' NOT NULL,
attachments int(11) unsigned DEFAULT '0' NOT NULL,
tstamp int(11) unsigned DEFAULT '0' NOT NULL,
crdate int(11) unsigned DEFAULT '0' NOT NULL,
......
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