1212use In2code \Lux \Utility \StringUtility ;
1313use In2code \Lux \Utility \UrlUtility ;
1414use Psr \EventDispatcher \EventDispatcherInterface ;
15- use TYPO3 \CMS \Core \Mail \MailMessage ;
15+ use Symfony \Component \Mime \Address ;
16+ use TYPO3 \CMS \Core \Http \ServerRequest ;
17+ use TYPO3 \CMS \Core \Mail \FluidEmail ;
18+ use TYPO3 \CMS \Core \Mail \MailerInterface ;
1619use TYPO3 \CMS \Core \Resource \File ;
1720use TYPO3 \CMS \Core \Resource \Security \FileNameValidator ;
1821use TYPO3 \CMS \Core \Resource \StorageRepository ;
1922use TYPO3 \CMS \Core \Utility \GeneralUtility ;
2023use TYPO3 \CMS \Extbase \Configuration \Exception \InvalidConfigurationTypeException ;
21- use TYPO3 \CMS \Fluid \View \StandaloneView ;
2224
2325class SendAssetEmail4LinkService
2426{
@@ -44,57 +46,46 @@ public function __construct(Visitor $visitor, array $settings)
4446 $ this ->eventDispatcher = GeneralUtility::makeInstance (EventDispatcherInterface::class);
4547 }
4648
47- /**
48- * @param string $href
49- * @param File|null $file
50- * @return void
51- * @throws InvalidConfigurationTypeException
52- */
5349 public function sendMail (string $ href , ?File $ file ): void
5450 {
5551 $ this ->href = $ href ;
5652 $ this ->file = $ file ;
5753 if ($ this ->visitor ->isNotBlacklisted ()) {
5854 if ($ this ->isActivatedAndAllowed ()) {
5955 $ this ->send ();
60- $ this ->eventDispatcher ->dispatch (
61- GeneralUtility::makeInstance (LogEmail4linkSendEmailEvent::class, $ this ->visitor , $ this ->href )
62- );
56+ $ this ->eventDispatcher ->dispatch (new LogEmail4linkSendEmailEvent ($ this ->visitor , $ this ->href ));
6357 } else {
64- $ this ->eventDispatcher ->dispatch (
65- GeneralUtility::makeInstance (LogEmail4linkSendEmailFailedEvent::class, $ this ->visitor , $ this ->href )
66- );
58+ $ this ->eventDispatcher ->dispatch (new LogEmail4linkSendEmailFailedEvent ($ this ->visitor , $ this ->href ));
6759 }
6860 }
6961 }
7062
71- /**
72- * @return void
73- * @throws InvalidConfigurationTypeException
74- */
7563 protected function send (): void
7664 {
77- $ message = GeneralUtility::makeInstance (MailMessage::class);
78- $ message
79- ->setTo ([$ this ->visitor ->getEmail () => 'Receiver ' ])
80- ->setFrom ($ this ->getSender ())
81- ->setSubject ($ this ->getSubject ())
82- ->attachFromPath (GeneralUtility::getFileAbsFileName (UrlUtility::convertToRelative ($ this ->href )))
83- ->html ($ this ->getMailTemplate ());
84- $ this ->setBcc ($ message );
85- $ this ->eventDispatcher ->dispatch (
86- GeneralUtility::makeInstance (SetAssetEmail4LinkEvent::class, $ this ->visitor , $ message , $ this ->href )
65+ /** @var SetAssetEmail4LinkEvent $event */
66+ $ event = $ this ->eventDispatcher ->dispatch (
67+ new SetAssetEmail4LinkEvent ($ this ->visitor , $ this ->href , $ this ->file )
8768 );
88- $ message ->send ();
69+ $ mail = GeneralUtility::makeInstance (FluidEmail::class);
70+ $ mail
71+ ->to ($ this ->visitor ->getEmail ())
72+ ->from ($ this ->getSender ())
73+ ->format (FluidEmail::FORMAT_BOTH )
74+ ->setTemplate ('Email4Link ' )
75+ ->assignMultiple ([
76+ 'href ' => $ event ->getHref (),
77+ 'visitor ' => $ event ->getVisitor (),
78+ 'file ' => $ event ->getFile (),
79+ 'request ' => $ this ->getRequest (),
80+ ])
81+ ->attachFromPath (GeneralUtility::getFileAbsFileName (UrlUtility::convertToRelative ($ event ->getHref ())));
82+ $ this ->setBcc ($ mail );
83+ GeneralUtility::makeInstance (MailerInterface::class)->send ($ mail );
8984 }
9085
91- /**
92- * @param MailMessage $message
93- * @return void
94- */
95- protected function setBcc (MailMessage $ message ): void
86+ protected function setBcc (FluidEmail $ mail ): void
9687 {
97- if (! empty ($ this ->settings ['identification ' ]['email4link ' ]['mail ' ]['bccEmail ' ]) ) {
88+ if (($ this ->settings ['identification ' ]['email4link ' ]['mail ' ]['bccEmail ' ] ?? '' ) !== '' ) {
9889 $ bcc = [];
9990 $ emails = GeneralUtility::trimExplode (
10091 ', ' ,
@@ -103,55 +94,19 @@ protected function setBcc(MailMessage $message): void
10394 );
10495 foreach ($ emails as $ email ) {
10596 if (GeneralUtility::validEmail ($ email )) {
106- $ bcc = array_merge ( $ bcc , [ $ email => ' Receiver ' ] );
97+ $ bcc[] = new Address ( $ email );
10798 }
10899 }
109- $ message -> setBcc ( $ bcc );
100+ $ mail -> bcc (... $ bcc );
110101 }
111102 }
112103
113- /**
114- * @return string
115- * @throws InvalidConfigurationTypeException
116- */
117- protected function getMailTemplate (): string
118- {
119- $ mailTemplatePath = $ this ->configurationService ->getTypoScriptSettingsByPath (
120- 'identification.email4link.mail.mailTemplate '
121- );
122- $ standaloneView = GeneralUtility::makeInstance (StandaloneView::class);
123- $ standaloneView ->setTemplatePathAndFilename (GeneralUtility::getFileAbsFileName ($ mailTemplatePath ));
124- $ standaloneView ->assignMultiple ([
125- 'href ' => $ this ->href ,
126- 'visitor ' => $ this ->visitor ,
127- 'file ' => $ this ->file ,
128- ]);
129- return $ standaloneView ->render ();
130- }
131-
132- /**
133- * @return array
134- * @throws InvalidConfigurationTypeException
135- */
136- protected function getSender (): array
104+ protected function getSender (): Address
137105 {
138106 $ configuration = $ this ->configurationService ->getTypoScriptSettingsByPath ('identification.email4link.mail ' );
139- return [ $ configuration ['fromEmail ' ] => $ configuration ['fromName ' ]] ;
107+ return new Address ( $ configuration ['fromEmail ' ], $ configuration ['fromName ' ]) ;
140108 }
141109
142- /**
143- * @return string
144- * @throws InvalidConfigurationTypeException
145- */
146- protected function getSubject (): string
147- {
148- return $ this ->configurationService ->getTypoScriptSettingsByPath ('identification.email4link.mail.subject ' );
149- }
150-
151- /**
152- * @return bool
153- * @throws InvalidConfigurationTypeException
154- */
155110 protected function isActivatedAndAllowed (): bool
156111 {
157112 return $ this ->isEnabled ()
@@ -162,20 +117,12 @@ protected function isActivatedAndAllowed(): bool
162117 && $ this ->visitor ->isIdentified ();
163118 }
164119
165- /**
166- * @return bool
167- * @throws InvalidConfigurationTypeException
168- */
169120 protected function isEnabled (): bool
170121 {
171122 $ path = 'identification.email4link.mail._enable ' ;
172123 return $ this ->configurationService ->getTypoScriptSettingsByPath ($ path ) === '1 ' ;
173124 }
174125
175- /**
176- * @return bool
177- * @throws InvalidConfigurationTypeException
178- */
179126 protected function isAllowedFileExtension (): bool
180127 {
181128 $ allowed = false ;
@@ -221,4 +168,9 @@ protected function isFileExisting(): bool
221168 {
222169 return file_exists (GeneralUtility::getFileAbsFileName (UrlUtility::convertToRelative ($ this ->href )));
223170 }
171+
172+ protected function getRequest (): ?ServerRequest
173+ {
174+ return $ GLOBALS ['TYPO3_REQUEST ' ] ?? null ;
175+ }
224176}
0 commit comments