55use Doctrine \ORM \EntityManagerInterface ;
66use NTI \EmailBundle \Entity \Email ;
77use NTI \EmailBundle \Entity \Smtp ;
8+ use Swift_FileSpool ;
89use Symfony \Bundle \FrameworkBundle \Templating \EngineInterface ;
910use Symfony \Component \Console \Output \OutputInterface ;
1011use Symfony \Component \DependencyInjection \ContainerInterface ;
@@ -115,17 +116,40 @@ public function check(OutputInterface $output = null) {
115116
116117 $ em = $ this ->container ->get ('doctrine ' )->getManager ();
117118
118- /** @var Smtp $smtp */
119- $ smtp = $ em ->getRepository ('NTIEmailBundle:Smtp ' )->findOneBy (array ("environment " => $ this ->container ->getParameter ('app_env ' )));
120- if (!$ smtp ) {
119+ $ configurations = $ em ->getRepository ('NTIEmailBundle:Smtp ' )->findBy (array ("environment " => $ this ->container ->getParameter ('app_env ' )));
120+ if (empty ($ configurations )){
121121 if ($ this ->container ->has ('nti.logger ' )) {
122- $ this ->container ->get ('nti.logger ' )->logError ("Unable to find an SMTP configuration for this environment. " );
122+ $ this ->container ->get ('nti.logger ' )->logError ("No SMTP configuration found for this environment. " );
123123 }
124124 return false ;
125125 }
126- $ spoolFolder = $ this ->container ->getParameter ('swiftmailer.spool.default.file.path ' );
126+
127+ /** @var Smtp $smtp */
128+ foreach ($ configurations as $ smtp ){
129+ $ this ->handleSmtpSpool ($ smtp , $ output );
130+ }
131+
132+ }
133+
134+ /**
135+ * @param Smtp $smtp
136+ * @param OutputInterface|null $output
137+ * @return bool|void
138+ * @throws \Swift_IoException
139+ */
140+ public function handleSmtpSpool (Smtp $ smtp , OutputInterface $ output = null ){
141+
142+ if (!$ smtp ) {
143+ return false ;
144+ }
145+
146+ $ em = $ this ->container ->get ('doctrine ' )->getManager ();
147+
148+ // Spool Directory
149+ $ spoolFolder = $ smtp ->getSpoolDir ();
127150 // Send Emails
128151 //create an instance of the spool object pointing to the right position in the filesystem
152+ /** @var Swift_FileSpool $spool */
129153 $ spool = new \Swift_FileSpool ($ spoolFolder );
130154 //create a new instance of Swift_SpoolTransport that accept an argument as Swift_FileSpool
131155 $ transport = \Swift_SpoolTransport::newInstance ($ spool );
@@ -142,11 +166,11 @@ public function check(OutputInterface $output = null) {
142166 $ spool ->setMessageLimit (10 );
143167 $ spool ->setTimeLimit (100 );
144168 $ sent = $ spool ->flushQueue ($ realTransport );
145- $ output ->writeln ("Sent " .$ sent ." emails. " );
169+ $ output ->writeln ("Sent " .$ sent ." emails with config: { $ smtp -> getUniqueId ()} . " );
146170 // Check email statuses
147- $ emails = $ em ->getRepository ('NTIEmailBundle:Email ' )->findEmailsToCheck ( );
171+ $ emails = $ em ->getRepository ('NTIEmailBundle:Email ' )->findEmailsToCheckByConfigName ( $ smtp -> getUniqueId () );
148172 if (count ($ emails ) <= 0 ) {
149- $ output ->writeln ("No emails to check... " );
173+ $ output ->writeln ("No emails to check with config: { $ smtp -> getUniqueId ()} . ... " );
150174 return ;
151175 }
152176 /** @var Email $email */
@@ -196,7 +220,7 @@ public function check(OutputInterface $output = null) {
196220 }
197221 }
198222 }
199- // C1heck if it failed
223+ // Check if it failed
200224 if (file_exists ($ spoolFolder ."/ " .$ email ->getFilename ().".failure " )) {
201225 // Attempt to reset it
202226 @rename ($ spoolFolder ."/ " .$ email ->getFilename ().".failure " , $ spoolFolder ."/ " .$ email ->getFilename ());
@@ -212,10 +236,10 @@ public function check(OutputInterface $output = null) {
212236 try {
213237 $ em ->flush ();
214238 if ($ this ->container ->has ('nti.logger ' )) {
215- $ this ->container ->get ('nti.logger ' )->logDebug ("Finished checking " .count ($ emails )." emails. " );
239+ $ this ->container ->get ('nti.logger ' )->logDebug ("Finished checking " .count ($ emails )." emails with config: { $ smtp -> getUniqueId ()} . . " );
216240 }
217241 } catch (\Exception $ ex ) {
218- $ output ->writeln ("An error occurred while checking the emails. . " );
242+ $ output ->writeln ("An error occurred while checking the emails with config: { $ smtp -> getUniqueId ()} . " );
219243 if ($ this ->container ->has ('nti.logger ' )) {
220244 $ this ->container ->get ('nti.logger ' )->logException ($ ex );
221245 }
@@ -365,19 +389,19 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
365389 $ em = $ this ->container ->get ('doctrine ' )->getManager ();
366390
367391 /** @var Smtp $smtp */
368- $ smtp = $ em ->getRepository ('NTIEmailBundle:Smtp ' )->findOneBy (array ("environment " => $ environment ));
392+ $ smtp = $ em ->getRepository ('NTIEmailBundle:Smtp ' )->findOneBy (array ("environment " => $ environment, " uniqueId " => strtolower ( $ from ) ));
369393
370394 if (!$ smtp ) {
371395 if ($ this ->container ->has ('nti.logger ' )) {
372- $ this ->container ->get ('nti.logger ' )->logError ("Unable to find an SMTP configuration for this environment. " );
396+ $ this ->container ->get ('nti.logger ' )->logError ("Unable to find an SMTP configuration for this environment and { $ from } . " );
373397 }
374398 return false ;
375399 }
376400
377401
378402 // Create a new temporary spool
379403 $ hash = md5 (uniqid (time ()));
380- $ tempSpoolPath = $ this -> container -> getParameter ( ' swiftmailer.spool.default.file.path ' )."/ " .$ hash ."/ " ;
404+ $ tempSpoolPath = $ smtp -> getSpoolDir ( )."/ " .$ hash ."/ " ;
381405 $ tempSpool = new \Swift_FileSpool ($ tempSpoolPath );
382406
383407 /** @var \Swift_Mailer $mailer */
@@ -393,7 +417,7 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
393417 $ files = scandir ($ tempSpoolPath , SORT_ASC );
394418 if (count ($ files ) <= 0 ) {
395419 if ($ this ->container ->has ('nti.logger ' )){
396- $ this ->container ->get ('nti.logger ' )->logError ("Unable to find file in temporary spool... " );
420+ $ this ->container ->get ('nti.logger ' )->logError ("Unable to find file in temporary spool with config: { $ smtp -> getUniqueId ()} ... " );
397421 }
398422 }
399423 $ filename = null ;
@@ -423,7 +447,7 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
423447 $ from = (is_array ($ message ->getFrom ())) ? join (', ' , array_keys ($ message ->getFrom ())) : $ message ->getFrom ();
424448 $ recipients = (is_array ($ message ->getTo ())) ? join (', ' , array_keys ($ message ->getTo ())) : $ message ->getTo ();
425449 $ email ->setFilename ($ filename );
426- $ email ->setPath ($ this -> container -> getParameter ( ' swiftmailer.spool.default.file.path ' )."/ " );
450+ $ email ->setPath ($ smtp -> getSpoolDir ( )."/ " );
427451 $ email ->setMessageFrom ($ from );
428452 $ email ->setMessageTo ($ recipients );
429453 $ email ->setMessageSubject ($ message ->getSubject ());
0 commit comments