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

namespace SGalinski\SgMail\Service;

5
6
use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Repository\MailRepository;
7
use TYPO3\CMS\Core\Mail\MailMessage;
8
use TYPO3\CMS\Core\Utility\GeneralUtility;
Torsten Oppermann's avatar
Torsten Oppermann committed
9
use TYPO3\CMS\Extbase\Object\ObjectManager;
10
use TYPO3\CMS\Fluid\View\StandaloneView;
11
12
use TYPO3\CMS\Lang\LanguageService;
use Typoheads\Formhandler\Finisher\AbstractFinisher;
13

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

/**
 * MailTemplateService
 */
41
class MailTemplateService extends AbstractFinisher {
42

43
44
45
	/**
	 * @var array
	 */
46
	private static $registerArray = [];
47

48
	/**
49
	 * Hook into Formhandler, sets config and form input
50
	 * gets called in the finisher of the typoscript
51
	 * redirects to process() function
52
	 *
53
	 * @param $formInput
54
55
	 * @param $tsConfig
	 */
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	public function init($formInput, $tsConfig) {
		$this->gp = $formInput;
		foreach ($formInput as &$userInput) {
			GeneralUtility::removeXSS($userInput);
		}

		$this->tsConfig = $tsConfig;
	}

	/**
	 *  Redirect Formhandler input to send Email Function
	 *  Needed to hook into Formhandler
	 */
	public function process() {
		/** @var $translator LanguageService */
		$translator = (TYPO3_MODE === 'FE' ? $GLOBALS['TSFE'] : $GLOBALS['LANG']);
		$subject = $translator->sL($this->tsConfig['subject'], TRUE);

74
		$toAddress = $this->tsConfig['to_address'];
75
		if ($toAddress === 'email') {
76
77
78
			$toAddress = $this->gp['email'];
		}

79
		self::sendEmail(
80
			'en', $this->tsConfig['template_key'], $this->tsConfig['extension_key'], $toAddress,
81
			$this->tsConfig['from_address'], $subject, $this->gp, $this->tsConfig['ignore_mail_queue']
82
		);
83
84
85
86
87
88
89
90
91
		return $this->gp;
	}

	/**
	 * Needed to hook into Formhandler
	 * simply returns true
	 */
	public function validateConfig() {
		parent::validateConfig();
92
93
	}

Torsten Oppermann's avatar
Torsten Oppermann committed
94
	/**
Torsten Oppermann's avatar
Torsten Oppermann committed
95
	 * register a template with sg_mail
Fabian Galinski's avatar
Fabian Galinski committed
96
	 *
97
	 * @param string $extension
Torsten Oppermann's avatar
Torsten Oppermann committed
98
99
100
	 * @param string $templateName
	 * @param string $templatePath
	 * @param string $description
101
102
	 * @param array $marker
	 * @return string
Torsten Oppermann's avatar
Torsten Oppermann committed
103
	 */
104
105
	public static function registerTemplate($extension, $templateName, $templatePath, $description, array $marker) {

106
		MailTemplateService::$registerArray[$extension][$templateName] = [
107
			'templatePath' => $templatePath,
108
109
			'description' => $description,
			'marker' => $marker,
110
111
			'extension' => $extension,
			'templateName' => $templateName
112
		];
113

114
		$defaultLangFile = $templatePath . 'en.sg_mail.locallang.html';
115
		if (!file_exists($defaultLangFile)) {
116
			$langFileContent = '';
117
			file_put_contents($defaultLangFile, $langFileContent);
118
		}
119
	}
120
121

	/**
Torsten Oppermann's avatar
Torsten Oppermann committed
122
	 * Get all registered templates
Fabian Galinski's avatar
Fabian Galinski committed
123
	 *
124
	 * @return array
125
	 */
126
127
	public static function getRegisterArray() {
		return self::$registerArray;
128
	}
129
130

	/**
Torsten Oppermann's avatar
Torsten Oppermann committed
131
	 * send an Email
Fabian Galinski's avatar
Fabian Galinski committed
132
	 *
133
134
135
136
137
138
	 * @param string $language
	 * @param string $templateKey
	 * @param string $extensionKey
	 * @param string $toAddress
	 * @param string $fromAddress
	 * @param string $subject
Torsten Oppermann's avatar
Torsten Oppermann committed
139
	 * @param array $content
140
	 * @param boolean $ignoreMailQueue
141
142
	 */
	public static function sendEmail(
143
		$language, $templateKey, $extensionKey, $toAddress, $fromAddress, $subject,
144
		array $content = [],
145
		$ignoreMailQueue = FALSE
146
147
148
149
150
	) {
		// Set Email Content
		$registerArray = MailTemplateService::getRegisterArray();
		$templateEntry = $registerArray[$extensionKey][$templateKey];

151
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
Torsten Oppermann's avatar
Torsten Oppermann committed
152
		$emailView = $objectManager->get(StandaloneView::class);
153
154
155
		$emailView->setTemplatePathAndFilename($templateEntry['templatePath'] . $language . '.sg_mail.locallang.html');
		$emailView->assignMultiple($content);
		$emailBody = $emailView->render();
156

157
158
159
160
161
162
163
164
165
166
167
		if ($ignoreMailQueue) {
			$mailMessage = $objectManager->get(MailMessage::class);
			$mailMessage->setFrom($fromAddress);
			$mailMessage->setTo($toAddress);
			$mailMessage->setSubject($subject);
			$mailMessage->setBody($emailBody, 'text/html');
			$mailMessage->send();
		} else {
			// @todo evtl. sollten hier noch die Parameter $priority und $pid mit gegeben werden.
			self::addMailToMailQueue($fromAddress, $toAddress, $subject, $emailBody);
		}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	}

	/**
	 * 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);
200
	}
201
}