55namespace PhpList \Core \Domain \Subscription \Service ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use PhpList \Core \Domain \Configuration \Model \ConfigOption ;
9+ use PhpList \Core \Domain \Configuration \Service \Provider \ConfigProvider ;
810use PhpList \Core \Domain \Subscription \Exception \CouldNotReadUploadedFileException ;
911use PhpList \Core \Domain \Subscription \Model \Dto \ImportSubscriberDto ;
1012use PhpList \Core \Domain \Subscription \Model \Dto \SubscriberImportOptions ;
1416use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberAttributeManager ;
1517use PhpList \Core \Domain \Subscription \Service \Manager \SubscriberManager ;
1618use PhpList \Core \Domain \Subscription \Service \Manager \SubscriptionManager ;
19+ use PhpList \Core \Domain \Messaging \Service \EmailService ;
20+ use PhpList \Core \Domain \Subscription \Repository \SubscriberListRepository ;
21+ use Symfony \Component \Mime \Email ;
1722use Symfony \Component \HttpFoundation \File \UploadedFile ;
1823use Symfony \Contracts \Translation \TranslatorInterface ;
1924use Throwable ;
@@ -32,6 +37,9 @@ class SubscriberCsvImporter
3237 private SubscriberAttributeDefinitionRepository $ attrDefinitionRepository ;
3338 private EntityManagerInterface $ entityManager ;
3439 private TranslatorInterface $ translator ;
40+ private EmailService $ emailService ;
41+ private ConfigProvider $ configProvider ;
42+ private SubscriberListRepository $ subscriberListRepository ;
3543
3644 public function __construct (
3745 SubscriberManager $ subscriberManager ,
@@ -42,6 +50,9 @@ public function __construct(
4250 SubscriberAttributeDefinitionRepository $ attrDefinitionRepository ,
4351 EntityManagerInterface $ entityManager ,
4452 TranslatorInterface $ translator ,
53+ EmailService $ emailService ,
54+ ConfigProvider $ configProvider ,
55+ SubscriberListRepository $ subscriberListRepository ,
4556 ) {
4657 $ this ->subscriberManager = $ subscriberManager ;
4758 $ this ->attributeManager = $ attributeManager ;
@@ -51,6 +62,9 @@ public function __construct(
5162 $ this ->attrDefinitionRepository = $ attrDefinitionRepository ;
5263 $ this ->entityManager = $ entityManager ;
5364 $ this ->translator = $ translator ;
65+ $ this ->emailService = $ emailService ;
66+ $ this ->configProvider = $ configProvider ;
67+ $ this ->subscriberListRepository = $ subscriberListRepository ;
5468 }
5569
5670 /**
@@ -83,9 +97,6 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
8397 foreach ($ result ['valid ' ] as $ dto ) {
8498 try {
8599 $ this ->processRow ($ dto , $ options , $ stats );
86- if (!$ options ->dryRun ) {
87- $ this ->entityManager ->flush ();
88- }
89100 } catch (Throwable $ e ) {
90101 $ stats ['errors ' ][] = $ this ->translator ->trans (
91102 'Error processing %email%: %error% ' ,
@@ -179,6 +190,37 @@ private function processRow(
179190 $ this ->subscriptionManager ->addSubscriberToAList ($ subscriber , $ listId );
180191 }
181192 }
193+
194+ if (!$ options ->dryRun ) {
195+ $ this ->entityManager ->flush ();
196+ if ($ this ->configProvider ->isEnabled (ConfigOption::SendSubscribeMessage)) {
197+ $ this ->sendSubscribeEmail ($ dto ->email , $ options ->listIds );
198+ }
199+ }
200+ }
201+
202+ private function sendSubscribeEmail (string $ subscriberEmail , array $ listIds ): void
203+ {
204+ $ listNames = [];
205+ foreach ($ listIds as $ id ) {
206+ $ list = $ this ->subscriberListRepository ->find ($ id );
207+ if ($ list ) {
208+ $ listNames [] = $ list ->getName ();
209+ }
210+ }
211+ $ listOfLists = implode (', ' , $ listNames );
212+
213+ $ subject = $ this ->configProvider ->getValue ('subscribesubject ' , 'Subscription ' );
214+ $ message = $ this ->configProvider ->getValue ('subscribemessage ' , 'You have been subscribed to: [LISTS] ' );
215+ $ message = str_replace ('[LISTS] ' , $ listOfLists , (string )$ message );
216+
217+ $ email = (new Email ())
218+ ->to ($ subscriberEmail )
219+ ->subject ((string )$ subject )
220+ ->text ($ message )
221+ ->html (nl2br (htmlentities ($ message )));
222+
223+ $ this ->emailService ->sendEmail ($ email );
182224 }
183225
184226 /**
0 commit comments