99use Contao \CoreBundle \Event \MemberActivationMailEvent ;
1010use Contao \CoreBundle \OptIn \OptInInterface ;
1111use Contao \CoreBundle \OptIn \OptInToken ;
12- use Contao \Email ;
12+ use Contao \CoreBundle \Routing \ContentUrlGenerator ;
13+ use Contao \CoreBundle \String \SimpleTokenParser ;
1314use Contao \Environment ;
1415use Contao \FilesModel ;
1516use Contao \Folder ;
2324use Contao \System ;
2425use Contao \Versions ;
2526use Psr \Log \LoggerInterface ;
27+ use Symfony \Component \DependencyInjection \Attribute \Autowire ;
2628use Symfony \Component \HttpFoundation \RedirectResponse ;
2729use Symfony \Component \HttpFoundation \Request ;
30+ use Symfony \Component \Mailer \MailerInterface ;
31+ use Symfony \Component \Mime \Address ;
32+ use Symfony \Component \Mime \Email ;
2833use Symfony \Component \Routing \RouterInterface ;
2934use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
3035use Symfony \Contracts \Translation \TranslatorInterface ;
@@ -35,7 +40,14 @@ public function __construct(
3540 private readonly OptInInterface $ optIn ,
3641 private readonly EventDispatcherInterface $ eventDispatcher ,
3742 private readonly RouterInterface $ router ,
43+ private readonly ContentUrlGenerator $ contentUrlGenerator ,
44+ private readonly SimpleTokenParser $ simpleTokenParser ,
45+ private readonly MailerInterface $ mailer ,
3846 private readonly TranslatorInterface $ translator ,
47+ #[Autowire(param: 'contao.registration.expiration ' )]
48+ private readonly int $ registrationExpiration ,
49+ #[Autowire(param: 'kernel.project_dir ' )]
50+ private readonly string $ projectDir ,
3951 private readonly ?LoggerInterface $ logger = null ,
4052 ) {
4153 }
@@ -91,7 +103,7 @@ public function createMember(array $data, ContentModel $model, Request $request)
91103 }
92104
93105 if ($ target = PageModel::findById ($ model ->msrJumpTo ?? null )) {
94- return new RedirectResponse (System:: getContainer ()-> get ( ' contao.routing.content_url_generator ' ) ->generate ($ target ));
106+ return new RedirectResponse ($ this -> contentUrlGenerator ->generate ($ target ));
95107 }
96108
97109 return null ;
@@ -145,7 +157,7 @@ public function activateAccount(string $token, ContentModel $model): array
145157 $ redirect = null ;
146158
147159 if ($ target = PageModel::findById ($ model ->msrRegJumpTo ?? null )) {
148- $ redirect = new RedirectResponse (System:: getContainer ()-> get ( ' contao.routing.content_url_generator ' ) ->generate ($ target ));
160+ $ redirect = new RedirectResponse ($ this -> contentUrlGenerator ->generate ($ target ));
149161 }
150162
151163 return [
@@ -160,18 +172,22 @@ public function activateAccount(string $token, ContentModel $model): array
160172 */
161173 private function sendActivationMail (array $ data , ContentModel $ model , Request $ request ): void
162174 {
163- $ removeOn = new \DateTime ('+ ' .System:: getContainer ()-> getParameter ( ' contao.registration.expiration ' ) .' days ' );
175+ $ removeOn = new \DateTime ('+ ' .$ this -> registrationExpiration .' days ' );
164176 $ optInToken = $ this ->optIn ->create ('reg ' , (string ) $ data ['email ' ], ['tl_member ' => [$ data ['id ' ]]]);
165177
166- if ($ optInModel = OptInModel::findByToken ($ optInToken ->getIdentifier ())) {
167- $ optInModel ->removeOn = $ removeOn ->getTimestamp ();
168- $ optInModel ->save ();
178+ if (!$ optInToken instanceof OptInToken) {
179+ return ;
169180 }
170181
171- if (!$ optInToken instanceof OptInToken) {
182+ $ optInModel = OptInModel::findByToken ($ optInToken ->getIdentifier ());
183+
184+ if (null === $ optInModel ) {
172185 return ;
173186 }
174187
188+ $ optInModel ->removeOn = $ removeOn ->getTimestamp ();
189+ $ optInModel ->save ();
190+
175191 $ uri = $ request ->getUri ();
176192
177193 $ tokenData = $ data ;
@@ -190,8 +206,11 @@ private function sendActivationMail(array $data, ContentModel $model, Request $r
190206 $ this ->eventDispatcher ->dispatch ($ event );
191207
192208 if ($ event ->shouldSendOptInToken ()) {
193- $ text = System::getContainer ()->get ('contao.string.simple_token_parser ' )->parse ($ event ->getText (), $ event ->getSimpleTokens ());
194- $ optInToken ->send ($ event ->getSubject (), $ text );
209+ $ optInModel ->emailSubject = $ event ->getSubject ();
210+ $ optInModel ->emailText = $ this ->simpleTokenParser ->parse ($ event ->getText (), $ event ->getSimpleTokens ());
211+ $ optInModel ->save ();
212+
213+ $ this ->sendOptInMail ($ optInModel );
195214 }
196215 }
197216
@@ -207,9 +226,8 @@ private function assignHomeDirectory(MemberModel $member, array $data, ContentMo
207226 }
208227
209228 $ userDir = StringUtil::standardize ((string ) ($ data ['username ' ] ?? '' )) ?: 'user_ ' .$ member ->id ;
210- $ projectDir = System::getContainer ()->getParameter ('kernel.project_dir ' );
211229
212- while (is_dir ($ projectDir .'/ ' .$ homeDir ->path .'/ ' .$ userDir )) {
230+ while (is_dir ($ this -> projectDir .'/ ' .$ homeDir ->path .'/ ' .$ userDir )) {
213231 $ userDir .= '_ ' .$ member ->id ;
214232 }
215233
@@ -238,13 +256,29 @@ private function resendActivationMail(MemberModel $member): void
238256 $ token = $ this ->optIn ->find ($ model ->token );
239257
240258 if ($ token && $ token ->isValid () && !$ token ->isConfirmed ()) {
241- $ token -> send ( );
259+ $ this -> sendOptInMail ( $ model );
242260
243261 return ;
244262 }
245263 }
246264 }
247265
266+ private function sendOptInMail (OptInModel $ model ): void
267+ {
268+ if (!$ model ->emailSubject || !$ model ->emailText ) {
269+ throw new \LogicException ('Please provide subject and text to send the token ' );
270+ }
271+
272+ $ email = new Email ()
273+ ->from ($ this ->getSender ())
274+ ->to ((string ) $ model ->email )
275+ ->subject ((string ) $ model ->emailSubject )
276+ ->html ((string ) $ model ->emailText )
277+ ;
278+
279+ $ this ->mailer ->send ($ email );
280+ }
281+
248282 private function createHookModule (ContentModel $ model ): Module
249283 {
250284 $ moduleModel = new ModuleModel ();
@@ -265,7 +299,9 @@ private function sendAdminNotification(int|string $id, array $data): void
265299 {
266300 $ this ->logger ?->info('A new user (ID ' .$ id .') has registered on the website ' );
267301
268- if (!isset ($ GLOBALS ['TL_ADMIN_EMAIL ' ])) {
302+ $ adminEmail = $ GLOBALS ['TL_ADMIN_EMAIL ' ] ?? null ;
303+
304+ if (!\is_string ($ adminEmail ) || '' === $ adminEmail ) {
269305 return ;
270306 }
271307
@@ -285,12 +321,37 @@ private function sendAdminNotification(int|string $id, array $data): void
285321 $ messageData .= ($ GLOBALS ['TL_LANG ' ]['tl_member ' ][$ key ][0 ] ?? $ key ).': ' .(\is_array ($ value ) ? implode (', ' , $ value ) : $ value )."\n" ;
286322 }
287323
288- $ email = new Email ();
289- $ email ->from = $ GLOBALS ['TL_ADMIN_EMAIL ' ];
290- $ email ->fromName = $ GLOBALS ['TL_ADMIN_NAME ' ] ?? null ;
291- $ email ->subject = \sprintf ($ GLOBALS ['TL_LANG ' ]['MSC ' ]['adminSubject ' ], Idna::decode (Environment::get ('host ' )));
292- $ email ->text = \sprintf ($ GLOBALS ['TL_LANG ' ]['MSC ' ]['adminText ' ], $ id , $ messageData ."\n" )."\n" ;
293- $ email ->sendTo ($ GLOBALS ['TL_ADMIN_EMAIL ' ]);
324+ $ email = new Email ()
325+ ->from ($ this ->getSender ())
326+ ->to ($ adminEmail )
327+ ->subject (\sprintf ($ GLOBALS ['TL_LANG ' ]['MSC ' ]['adminSubject ' ], Idna::decode (Environment::get ('host ' ))))
328+ ->text (\sprintf ($ GLOBALS ['TL_LANG ' ]['MSC ' ]['adminText ' ], $ id , $ messageData ."\n" )."\n" )
329+ ;
330+
331+ $ this ->mailer ->send ($ email );
332+ }
333+
334+ private function getSender (): Address
335+ {
336+ $ adminEmail = $ GLOBALS ['TL_ADMIN_EMAIL ' ] ?? null ;
337+
338+ if (\is_string ($ adminEmail ) && '' !== $ adminEmail ) {
339+ $ adminName = $ GLOBALS ['TL_ADMIN_NAME ' ] ?? '' ;
340+
341+ return new Address ($ adminEmail , \is_string ($ adminName ) ? $ adminName : '' );
342+ }
343+
344+ $ adminEmail = Config::get ('adminEmail ' );
345+
346+ if (\is_string ($ adminEmail ) && '' !== $ adminEmail ) {
347+ [$ name , $ email ] = StringUtil::splitFriendlyEmail ($ adminEmail );
348+
349+ if (\is_string ($ email ) && '' !== $ email ) {
350+ return new Address ($ email , \is_string ($ name ) ? $ name : '' );
351+ }
352+ }
353+
354+ throw new \LogicException ('No administrator e-mail address has been set. ' );
294355 }
295356
296357 private function getModelValue (ContentModel $ model , string $ field ): mixed
0 commit comments