MailTemplateService.php 4.22 KB
Newer Older
1
2
3
4
<?php

namespace SGalinski\SgMail\Service;

5
6
7
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Repository\MailRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Torsten Oppermann's avatar
Torsten Oppermann committed
8
use TYPO3\CMS\Extbase\Object\ObjectManager;
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/***************************************************************
 *  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!
 ***************************************************************/
33
34
35
36
37
38

/**
 * MailTemplateService
 */
class MailTemplateService {

39
40
41
	/**
	 * @var array
	 */
42
	private static $registerArray = [];
43

Torsten Oppermann's avatar
Torsten Oppermann committed
44
	/**
Fabian Galinski's avatar
Fabian Galinski committed
45
46
	 * @todo Code-Dokumentation, Parameter vergessen bei param
	 *
47
	 * @param string $extension
48
49
50
51
	 * @param string $extensionKey
	 * @param string $template
	 * @param array $marker
	 * @return string
Torsten Oppermann's avatar
Torsten Oppermann committed
52
	 */
53
54
	public static function registerTemplate($extension, $templateName, $templatePath, $description, array $marker) {

55
		MailTemplateService::$registerArray[$extension][$extension . '_' . $templateName] = [
56
			'templatePath' => $templatePath,
57
58
			'description' => $description,
			'marker' => $marker,
59
60
			'extension' => $extension,
			'templateName' => $templateName
61
		];
62

63
		$defaultLangFile = $templatePath . 'en.sg_mail.locallang.html';
64
65
		if (!file_exists($defaultLangFile)) {
			$langFileContent = 'TEST';
66
			file_put_contents($defaultLangFile, $langFileContent);
67
		}
68
	}
69
70

	/**
Fabian Galinski's avatar
Fabian Galinski committed
71
72
	 * @todo Code-Dokumentation
	 *
73
	 * @return array
74
	 */
75
76
	public static function getRegisterArray() {
		return self::$registerArray;
77
	}
78
79

	/**
Fabian Galinski's avatar
Fabian Galinski committed
80
81
	 * @todo Code-Dokumentation, Parameter vergessen bei param
	 *
82
83
84
85
86
87
88
89
90
	 * @param string $language
	 * @param string $templateKey
	 * @param string $extensionKey
	 * @param string $toAddress
	 * @param string $fromAddress
	 * @param string $subject
	 * @param string $content
	 */
	public static function sendEmail(
91
		$language, $templateKey, $extensionKey, $toAddress, $fromAddress, $subject, array $content = []
92
93
94
95
96
	) {
		// Set Email Content
		$registerArray = MailTemplateService::getRegisterArray();
		$templateEntry = $registerArray[$extensionKey][$templateKey];

97
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
Fabian Galinski's avatar
Fabian Galinski committed
98
		// @todo verwende bitte StandaloneView::class
99
100
101
102
		$emailView = $objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
		$emailView->setTemplatePathAndFilename($templateEntry['templatePath'] . $language . '.sg_mail.locallang.html');
		$emailView->assignMultiple($content);
		$emailBody = $emailView->render();
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		// @todo evtl. sollten hier noch die Parameter $priority und $pid mit gegeben werden.
		self::addMailToMailQueue($fromAddress, $toAddress, $subject, $emailBody);
	}

	/**
	 * Adds a new mail to the mail queue. This will have the given parameters.
	 *
	 * @param string $fromAddress
	 * @param string $toAddress
	 * @param string $mailSubject
	 * @param string $mailBody
	 * @param int $priority
	 * @param int $pid
	 */
	public static function addMailToMailQueue(
		$fromAddress, $toAddress, $mailSubject, $mailBody = '', $priority = Mail::PRIORITY_LOWEST, $pid = 0
	) {
		/** @var ObjectManager $objectManager */
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);

		if ($pid <= 0 && isset($GLOBALS['TSFE'])) {
			$pid = (int) $GLOBALS['TSFE']->id;
		}

		$mail = $objectManager->get(Mail::class);
		$mail->setPid($pid);
		$mail->setFromAddress($fromAddress);
		$mail->setToAddress($toAddress);
		$mail->setMailSubject($mailSubject);
		$mail->setMailBody($mailBody);
		$mail->setPriority($priority);

		$mailRepository = $objectManager->get(MailRepository::class);
		$mailRepository->add($mail);
138
	}
139
}