Newer
Older
/**
* @param string $overwrittenEmailBody
*/
public function setOverwrittenEmailBody(string $overwrittenEmailBody) {
$this->overwrittenEmailBody = $overwrittenEmailBody;
}
/**
* Parses markers in an email View.
* !!! CHANGES THE SOURCE PATH AND IT SHOULD BE RESET BACK TO THE ORIGINAL!!!
*
* @param string $text
* @param StandaloneView $emailView
* @return mixed
*/
protected function parseMarkers($text, $emailView) {
$text = (string) $text;
if (strpos($text, '{') !== FALSE) {
$emailView->setTemplateSource($text);
return $emailView->render();

Torsten Oppermann
committed
*
* @param int $layoutUid
* @param int $siteRootId
* @return Layout|NULL
private function getLayoutSource(int $layoutUid, int $siteRootId) {
$languageUid = 0;
if ($this->language !== self::DEFAULT_LANGUAGE) {
$languageUid = (int) array_search($this->language, $this->getAvailableLanguages(), TRUE);
}
return $this->layoutRepository->findByUidOrDefault($layoutUid, $siteRootId, $languageUid);
* Returns the list of available translation languages
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
*
* @return array
*/
private function getAvailableLanguages(): array {
$out = [0 => ''];
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9.0.0', '<')) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(
'sys_language'
);
$rows = $queryBuilder->select('*')
->from('sys_language')->execute()->fetchAll();
foreach ($rows as $row) {
$out[(int) $row['uid']] = $row['language_isocode'];
}
} else {
try {
$site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId(0);
} catch (\Exception $exception) {
return [0 => ''];
}
$availableLanguages = $site->getLanguages();
$out = [];
foreach ($availableLanguages as $language) {
$languageId = $language->getLanguageId();
if ($languageId < 0) {
continue;
}
$out[$language->getLanguageId()] = strtolower($language->getTwoLetterIsoCode());
}
}
return $out;
}
* Adds a new mail to the mail queue.
* @param string $extensionKey
* @param string $templateName
* @param string $subject

Torsten Oppermann
committed
* @param int $sendingTime

Torsten Oppermann
committed
* @param int $lastSendingTime
* @param string $language
* @param int $pid
* @throws \InvalidArgumentException
* @throws \BadFunctionCallException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException

Torsten Oppermann
committed
private function addMailToMailQueue(
$extensionKey, $templateName, $subject, $emailBody, $priority, $sendingTime = 0,

Torsten Oppermann
committed
$lastSendingTime = 0, $language = self::DEFAULT_LANGUAGE, $pid = 0

Markus Guenther
committed
$mail = $this->objectManager->get(Mail::class);
$mail->setPid($pid);
$mail->setExtensionKey($extensionKey);
$mail->setTemplateName($templateName);
$mail->setLanguage($language);
$mail->setBlacklisted(self::isTemplateBlacklisted($extensionKey, $templateName, $pid));
$mail->setFromAddress($this->fromAddress);
$mail->setFromName($this->fromName);
$mail->setToAddress($this->toAddresses);
$mail->setMailSubject($subject);
$mail->setPriority($priority);
$mail->setBccAddresses($this->bccAddresses);
$mail->setCcAddresses($this->ccAddresses);

Torsten Oppermann
committed
$mail->setSendingTime($sendingTime);

Torsten Oppermann
committed
$mail->setLastSendingTime($lastSendingTime);
$mail->setReplyTo($this->replyToAddress);
foreach ($this->markers as $marker) {
if ($marker instanceof FileReference) {
// we need to create proper copies of the attachment so that the original file reference does not get
// moved over to the mail model and worst case, the original model loses the reference because of this
$originalResource = $marker->getOriginalResource();
if ($originalResource instanceof \TYPO3\CMS\Core\Resource\FileReference) {
$coreFileReferenceMailFile = $this->resourceFactory->createFileReferenceObject(
[
'uid_local' => $originalResource->getOriginalFile()->getUid(),
'table_local' => 'sys_file',
'uid' => uniqid('NEW_MAIL', TRUE)
]
);
$newFileReference = GeneralUtility::makeInstance(FileReference::class);
$newFileReference->setOriginalResource($coreFileReferenceMailFile);
$mail->addAttachment($newFileReference);
}

Markus Guenther
committed
$mailRepository = $this->objectManager->get(MailRepository::class);
$mailRepository->add($mail);
$this->persistenceManager->persistAll();
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
public function getSubjectToSend(): string {
return $this->subjectToSend;
}
/**
* @param string $subjectToSend
*/
public function setSubjectToSend(string $subjectToSend): void {
$this->subjectToSend = $subjectToSend;
}
/**
* @return string|string[]|null
*/
public function getMailBodyToSend() {
return $this->mailBodyToSend;
}
/**
* @param string|string[]|null $mailBodyToSend
*/
public function setMailBodyToSend($mailBodyToSend): void {
$this->mailBodyToSend = $mailBodyToSend;
/**
* Send a Mail from the queue, identified by its id
*
* @param int $uid
* @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
public function sendMailFromQueue($uid): bool {
$mailRepository = $this->objectManager->get(MailRepository::class);
/** @var Mail $mailToSend */
if (!isset(self::$mailObjectCache[$uid]) || !self::$mailObjectCache[$uid] instanceof Mail) {
$mailToSend = $this->getMailObjectByUid($uid);
if ($mailToSend === FALSE) {
return FALSE;
}
} else {
$mailToSend = self::$mailObjectCache[$uid];
}
$this->mailMessage->setBody($mailToSend->getMailBody(), 'text/html');
$plaintextService = GeneralUtility::makeInstance(PlaintextService::class);
$plaintextBody = $plaintextService->makePlain($mailToSend->getMailBody());
$this->mailMessage->addPart($plaintextBody, 'text/plain');
$toAddresses = \trim($mailToSend->getToAddress());
$addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE);
if (\count($addressesArray) > 1) {
$toAddresses = $addressesArray;
}
$this->mailMessage->setTo($toAddresses);
$this->mailMessage->setFrom($mailToSend->getFromAddress(), $mailToSend->getFromName());
$this->mailMessage->setSubject($mailToSend->getMailSubject());

Torsten Oppermann
committed
if ($mailToSend->getBccAddresses()) {
$this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $mailToSend->getBccAddresses()));
}

Torsten Oppermann
committed
if ($mailToSend->getCcAddresses()) {
$this->mailMessage->setCc(GeneralUtility::trimExplode(',', $mailToSend->getCcAddresses()));
}

Torsten Oppermann
committed
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());
}
$mailToSend->setLastSendingTime($dateTime->getTimestamp());
$success = $this->mailMessage->send();
if ($success) {
$mailRepository->update($mailToSend);
$this->persistenceManager->persistAll();
} else {
$this->mailMessage->getFailedRecipients();
}
unset(self::$mailObjectCache[$uid]); // free the memory
return $success;
/**
* Get the mail object by uid and check if it's blacklisted
Georgi Mateev
committed
*
* @param int $uid
* @return bool|object
Georgi Mateev
committed
*/
private function getMailObjectByUid($uid) {
$mailRepository = $this->objectManager->get(MailRepository::class);
$mailObject = $mailRepository->findOneByUid($uid);
if (!$mailObject || $mailObject->getBlacklisted()) {
return FALSE;
Georgi Mateev
committed
}
Georgi Mateev
committed
}