Skip to content

Commit f3e4f02

Browse files
author
ntidev
authored
Merge pull request #2 from ealcantara22/multiple-accounts
Multiple accounts support.
2 parents 6ca40ae + 2fe1a49 commit f3e4f02

File tree

5 files changed

+132
-21
lines changed

5 files changed

+132
-21
lines changed

Entity/Email.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Email
121121
*/
122122
private $attachments;
123123

124+
124125
public function __construct() {
125126
$this->status = self::STATUS_QUEUE;
126127
}
@@ -417,4 +418,5 @@ public function setAttachments($attachments)
417418
$this->attachments = $attachments;
418419
return $this;
419420
}
421+
420422
}

Entity/Smtp.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ class Smtp {
7575
*/
7676
private $environment;
7777

78+
/**
79+
* @var string
80+
*
81+
* @ORM\Column(name="unique_id", type="string", length=255, unique=true)
82+
*/
83+
private $uniqueId;
84+
85+
/**
86+
* @var string
87+
*
88+
* @ORM\Column(name="spool_dir", type="string", length=255)
89+
*/
90+
private $spoolDir;
91+
7892
/**
7993
* SMTP constructor.
8094
*/
@@ -216,4 +230,52 @@ public function setEnvironment($environment)
216230
}
217231

218232

233+
234+
/**
235+
* Set uniqueId
236+
*
237+
* @param string $uniqueId
238+
*
239+
* @return Smtp
240+
*/
241+
public function setUniqueId($uniqueId)
242+
{
243+
$this->uniqueId = $uniqueId;
244+
245+
return $this;
246+
}
247+
248+
/**
249+
* Get uniqueId
250+
*
251+
* @return string
252+
*/
253+
public function getUniqueId()
254+
{
255+
return $this->uniqueId;
256+
}
257+
258+
/**
259+
* Set spoolDir
260+
*
261+
* @param string $spoolDir
262+
*
263+
* @return Smtp
264+
*/
265+
public function setSpoolDir($spoolDir)
266+
{
267+
$this->spoolDir = $spoolDir;
268+
269+
return $this;
270+
}
271+
272+
/**
273+
* Get spoolDir
274+
*
275+
* @return string
276+
*/
277+
public function getSpoolDir()
278+
{
279+
return $this->spoolDir;
280+
}
219281
}

Repository/EmailRepository.php

100644100755
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,22 @@ public function findEmailsToCheck($limit = 10) {
2727
$qb->setMaxResults($limit);
2828
return $qb->getQuery()->getResult();
2929
}
30+
31+
/**
32+
* Return the last-checked $limit emails
33+
* that are not sent to get an update.
34+
*
35+
* @param $configName
36+
* @param int $limit
37+
* @return array
38+
*/
39+
public function findEmailsToCheckByConfigName($configName, $limit = 10) {
40+
$qb = $this->createQueryBuilder('e');
41+
$qb->andWhere('e.status != :sent')->setParameter("sent", Email::STATUS_SENT);
42+
$qb->andWhere('e.status != :failed')->setParameter("failed", Email::STATUS_FAILURE);
43+
$qb->andWhere('e.messageFrom != :messageFrom')->setParameter("messageFrom", strtolower($configName));
44+
$qb->addOrderBy('e.lastCheck', 'asc');
45+
$qb->setMaxResults($limit);
46+
return $qb->getQuery()->getResult();
47+
}
3048
}

Service/Mailer.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use NTI\EmailBundle\Entity\Email;
77
use NTI\EmailBundle\Entity\Smtp;
8+
use Swift_FileSpool;
89
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
910
use Symfony\Component\Console\Output\OutputInterface;
1011
use 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());

composer.json

100644100755
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
"name" : "nti/email-bundle",
33
"description" : "Symfony NTIEmailBundle",
44
"type" : "symfony-bundle",
5-
"authors" : [{
6-
"name" : "Benjamin Vison",
7-
"email" : "[email protected]"
8-
}],
5+
"authors" : [
6+
{
7+
"name" : "Benjamin Vison",
8+
"email" : "[email protected]"
9+
},{
10+
"name" : "Enercido Alcantara",
11+
"email" : "[email protected]"
12+
}
13+
],
914
"keywords" : [
10-
"LogBundle"
15+
"LogBundle", "EmailBundle"
1116
],
1217
"license" : [
1318
"MIT"

0 commit comments

Comments
 (0)