Newer
Older
$fromName = '';
if (!$isNewsletter && $template !== NULL) {
$fromName = $this->parseMarkers($template->getFromName(), $emailView);
} else if ($this->fromName === $this->defaultFromName) {
$fromName = $this->parseMarkers(
(empty($this->overwrittenFromName) && $template ? $template->getFromName(
) : $this->overwrittenFromName),
}
if ($fromName) { // we don't want to override the default if this value is empty here
$this->setFromName($fromName);
}
if (!$isNewsletter && $template !== NULL) {
$replyTo = $this->parseMarkers($template->getReplyTo(), $emailView);
$this->setReplyToAddress($replyTo);
} else if ($this->replyToAddress === '') {
$replyTo = $this->parseMarkers(
(empty($this->overwrittenReplyTo) && $template ? $template->getReplyTo(
) : $this->overwrittenReplyTo),
$emailView
);
$this->setReplyToAddress($replyTo);
}
if (!$isNewsletter && $template !== NULL) {
$cc = $this->parseMarkers($template->getCc(), $emailView);
if ($cc) {
$this->setCcAddresses($cc);
}
} else if (empty($this->ccAddresses)) {
$cc = $this->parseMarkers(
(empty($this->overwrittenCc) && $template ? $template->getCc() : $this->overwrittenCc),
$emailView
);
$this->setCcAddresses($cc);
}
if (!$isNewsletter && $template !== NULL) {
$bcc = $this->parseMarkers($template->getBcc(), $emailView);
if ($bcc) {
$this->setBccAddresses($bcc);
}
} else if (empty($this->bccAddresses)) {
$bcc = $this->parseMarkers(
(empty($this->overwrittenBcc) && $template ? $template->getBcc() : $this->overwrittenBcc),
$emailView
);
$this->setBccAddresses($bcc);
}
if (!$isNewsletter && $template !== NULL) {
$toAddress = $this->parseMarkers($template->getToAddress(), $emailView);
if ($toAddress) {
$this->setToAddresses($toAddress);
}
} else if ($this->toAddresses === '') {
$toAddress = $this->parseMarkers(
(empty($this->overwrittenToAddresses) && $template ? $template->getToAddress() : $this->overwrittenToAddresses),
$emailView
);
$this->setToAddresses($toAddress);
}
// reset template source back to default
$emailView->setTemplateSource(
$this->getTemplateSource(
empty($overwrittenEmailBody) ? $templateContent : $overwrittenEmailBody,
$layoutId, $siteRootId
)
);

Torsten Oppermann
committed
Stefan Galinski
committed
// insert <br> tags, but replace every instance of three or more successive breaks with just two.
$emailBody = $emailView->render();
$emailBody = \nl2br($emailBody);
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
$this->mailBodyToSend = \preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody);
}
/**
* Get site root ID
*
* @return int
*/
public function getSiteRootId(): int {
$pageUid = $this->getPageUid();
return BackendService::getSiteRoot($pageUid);
}
/**
* Send the Email
*
* @param bool $isPreview
* @param Template|null $template
* @return bool email was sent or added to mail queue successfully?
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public function sendEmail($isPreview = FALSE, $isNewsletter = FALSE): bool {
if ($isPreview) { //TODO: remove this from here
$this->setIgnoreMailQueue(TRUE);
}
$success = FALSE;
// Get page ID
// TODO: this doesn't belong here. The API user needs to provide the UID
$siteRootId = $this->getSiteRootId();
try {
$template = $this->getTemplate($siteRootId);
} catch (\Exception $e) {
return FALSE;
$registerService = GeneralUtility::makeInstance(RegisterService::class);
// get default template content from register array
$defaultTemplateContent = $this->getDefaultTemplateContent($template, $registerService);
// set the ToAddress if there are no placeholders in it
// TODO: does this belong here?
if ($template !== NULL && \filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) {
$this->setToAddresses(\trim($template->getToAddress()));
$this->extractValuesForMail($template, $registerService, $defaultTemplateContent, $siteRootId, $isNewsletter);
// Fallback empty recipient address to the default sender address
if (!$this->mailMessage->getTo()) {
$this->mailMessage->setTo($this->defaultFromAddress);
}
Stefan Galinski
committed
$mail = $this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $this->getSubjectToSend(), $this->getMailBodyToSend(),
$this->priority,
0, 0, $this->language, $siteRootId
);
self::$mailObjectCache[$mail->getUid()] = $mail; // add it to cache to avoid extra DB queries
if ($this->ignoreMailQueue) {
$success = $this->sendMailFromQueue($mail->getUid());
}
//TODO: this can be avoided if the sending logic is decoupled from this function
if ($isPreview) {
$mailRepository = $this->objectManager->get(MailRepository::class);
$mailRepository->remove($mail);
$this->persistenceManager->persistAll();

Torsten Oppermann
committed
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
/**
* Combines the template content with the layout and returns the result
*
* @param string $content
* @param int $layoutUid
* @param int $siteRootId
* @return string
*/
private function getTemplateSource(string $content, int $layoutUid, int $siteRootId): string {
if ($layoutUid === -1) {
return $content;
}
$languageUid = 0;
if ($this->language !== self::DEFAULT_LANGUAGE) {
$languageUid = (int) array_search($this->language, $this->getAvailableLanguages(), TRUE);
}
$frontendSimulated = FALSE;
if (!isset($GLOBALS['TSFE'])) {
$frontendSimulated = TRUE;
$GLOBALS['TSFE'] = new TypoScriptFrontendController(NULL, $siteRootId, 0);
}
/** @var array $layout */
$layout = $this->layoutRepository->findByUidOrDefault($layoutUid, $siteRootId, $languageUid);
if ($frontendSimulated) {
unset($GLOBALS['TSFE']);
}
if ($layout === NULL) {
return $content;
}
return str_replace('###CONTENT###', $content, $layout['content']);
}
/**
* Returns the list of avalilable translation languages
*
* @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();
/**
* Get the mail object by uid and check if it's blacklisted
*
* @param int $uid
* @return bool|object
*/
private function getMailObjectByUid($uid) {
$mailRepository = $this->objectManager->get(MailRepository::class);
$mailObject = $mailRepository->findOneByUid($uid);
if (!$mailObject || $mailObject->getBlacklisted()) {
return FALSE;
}
return $mailObject;
}
/**
* 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;
/**
* Sets the fromMail property of the mailTemplateService.
* Checks validity and uses all available fallbacks
*
* @param string $fromMail
* @return string
*/
private function getValidFromMail($fromMail): string {
$fromMail = \trim($fromMail);
if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) {
$fromMail = $this->fromAddress;
}
if (!\filter_var($fromMail, FILTER_VALIDATE_EMAIL)) {
$fromMail = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
if (!\filter_var($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'], FILTER_VALIDATE_EMAIL)) {
$fromMail = 'noreply@example.com';
}
}
return $fromMail;
/**
* Get a single variable containing a list of all markers
*
* @param array $markers
* @return string
*/
private function getAllMarker(array $markers): string {
$allMarker = '';
foreach ($markers as $key => $value) {
if (\array_key_exists($key, $this->markerLabels) && $this->markerLabels[$key] !== NULL) {
$key = $this->markerLabels[$key];
}
if (\is_string($value)) {
$allMarker .= $key . ': ' . $value . PHP_EOL;
} elseif (\is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
$allMarker .= $key . '.' . $innerKey . ': ' . $innerValue . PHP_EOL;
} elseif (\is_bool($value)) {
$valueAsString = $value ? 'true' : 'false';
$allMarker .= $key . ': ' . $valueAsString . PHP_EOL;
} elseif (\is_object($value)) {
if (\method_exists($value, '__toString')) {
$allMarker .= $key . ': ' . $value->__toString() . PHP_EOL;
}
}
}
return $allMarker;
}