Skip to content

Commit

Permalink
Merge pull request #2 from ealcantara22/multiple-accounts
Browse files Browse the repository at this point in the history
Multiple accounts support.
  • Loading branch information
ntidev authored Sep 14, 2018
2 parents 6ca40ae + 2fe1a49 commit f3e4f02
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Entity/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Email
*/
private $attachments;


public function __construct() {
$this->status = self::STATUS_QUEUE;
}
Expand Down Expand Up @@ -417,4 +418,5 @@ public function setAttachments($attachments)
$this->attachments = $attachments;
return $this;
}

}
62 changes: 62 additions & 0 deletions Entity/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ class Smtp {
*/
private $environment;

/**
* @var string
*
* @ORM\Column(name="unique_id", type="string", length=255, unique=true)
*/
private $uniqueId;

/**
* @var string
*
* @ORM\Column(name="spool_dir", type="string", length=255)
*/
private $spoolDir;

/**
* SMTP constructor.
*/
Expand Down Expand Up @@ -216,4 +230,52 @@ public function setEnvironment($environment)
}



/**
* Set uniqueId
*
* @param string $uniqueId
*
* @return Smtp
*/
public function setUniqueId($uniqueId)
{
$this->uniqueId = $uniqueId;

return $this;
}

/**
* Get uniqueId
*
* @return string
*/
public function getUniqueId()
{
return $this->uniqueId;
}

/**
* Set spoolDir
*
* @param string $spoolDir
*
* @return Smtp
*/
public function setSpoolDir($spoolDir)
{
$this->spoolDir = $spoolDir;

return $this;
}

/**
* Get spoolDir
*
* @return string
*/
public function getSpoolDir()
{
return $this->spoolDir;
}
}
18 changes: 18 additions & 0 deletions Repository/EmailRepository.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,22 @@ public function findEmailsToCheck($limit = 10) {
$qb->setMaxResults($limit);
return $qb->getQuery()->getResult();
}

/**
* Return the last-checked $limit emails
* that are not sent to get an update.
*
* @param $configName
* @param int $limit
* @return array
*/
public function findEmailsToCheckByConfigName($configName, $limit = 10) {
$qb = $this->createQueryBuilder('e');
$qb->andWhere('e.status != :sent')->setParameter("sent", Email::STATUS_SENT);
$qb->andWhere('e.status != :failed')->setParameter("failed", Email::STATUS_FAILURE);
$qb->andWhere('e.messageFrom != :messageFrom')->setParameter("messageFrom", strtolower($configName));
$qb->addOrderBy('e.lastCheck', 'asc');
$qb->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
}
56 changes: 40 additions & 16 deletions Service/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use NTI\EmailBundle\Entity\Email;
use NTI\EmailBundle\Entity\Smtp;
use Swift_FileSpool;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -115,17 +116,40 @@ public function check(OutputInterface $output = null) {

$em = $this->container->get('doctrine')->getManager();

/** @var Smtp $smtp */
$smtp = $em->getRepository('NTIEmailBundle:Smtp')->findOneBy(array("environment" => $this->container->getParameter('app_env')));
if (!$smtp) {
$configurations = $em->getRepository('NTIEmailBundle:Smtp')->findBy(array("environment" => $this->container->getParameter('app_env')));
if (empty($configurations)){
if ($this->container->has('nti.logger')) {
$this->container->get('nti.logger')->logError("Unable to find an SMTP configuration for this environment.");
$this->container->get('nti.logger')->logError("No SMTP configuration found for this environment.");
}
return false;
}
$spoolFolder = $this->container->getParameter('swiftmailer.spool.default.file.path');

/** @var Smtp $smtp */
foreach ($configurations as $smtp){
$this->handleSmtpSpool($smtp, $output);
}

}

/**
* @param Smtp $smtp
* @param OutputInterface|null $output
* @return bool|void
* @throws \Swift_IoException
*/
public function handleSmtpSpool(Smtp $smtp, OutputInterface $output = null){

if (!$smtp) {
return false;
}

$em = $this->container->get('doctrine')->getManager();

// Spool Directory
$spoolFolder = $smtp->getSpoolDir();
// Send Emails
//create an instance of the spool object pointing to the right position in the filesystem
/** @var Swift_FileSpool $spool */
$spool = new \Swift_FileSpool($spoolFolder);
//create a new instance of Swift_SpoolTransport that accept an argument as Swift_FileSpool
$transport = \Swift_SpoolTransport::newInstance($spool);
Expand All @@ -142,11 +166,11 @@ public function check(OutputInterface $output = null) {
$spool->setMessageLimit(10);
$spool->setTimeLimit(100);
$sent = $spool->flushQueue($realTransport);
$output->writeln("Sent ".$sent." emails.");
$output->writeln("Sent ".$sent." emails with config: {$smtp->getUniqueId()}.");
// Check email statuses
$emails = $em->getRepository('NTIEmailBundle:Email')->findEmailsToCheck();
$emails = $em->getRepository('NTIEmailBundle:Email')->findEmailsToCheckByConfigName($smtp->getUniqueId());
if(count($emails) <= 0) {
$output->writeln("No emails to check...");
$output->writeln("No emails to check with config: {$smtp->getUniqueId()}....");
return;
}
/** @var Email $email */
Expand Down Expand Up @@ -196,7 +220,7 @@ public function check(OutputInterface $output = null) {
}
}
}
// C1heck if it failed
// Check if it failed
if(file_exists($spoolFolder."/".$email->getFilename().".failure")) {
// Attempt to reset it
@rename($spoolFolder."/".$email->getFilename().".failure", $spoolFolder."/".$email->getFilename());
Expand All @@ -212,10 +236,10 @@ public function check(OutputInterface $output = null) {
try {
$em->flush();
if($this->container->has('nti.logger')) {
$this->container->get('nti.logger')->logDebug("Finished checking ".count($emails)." emails.");
$this->container->get('nti.logger')->logDebug("Finished checking ".count($emails)." emails with config: {$smtp->getUniqueId()}..");
}
} catch (\Exception $ex) {
$output->writeln("An error occurred while checking the emails..");
$output->writeln("An error occurred while checking the emails with config: {$smtp->getUniqueId()}.");
if($this->container->has('nti.logger')) {
$this->container->get('nti.logger')->logException($ex);
}
Expand Down Expand Up @@ -365,19 +389,19 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
$em = $this->container->get('doctrine')->getManager();

/** @var Smtp $smtp */
$smtp = $em->getRepository('NTIEmailBundle:Smtp')->findOneBy(array("environment" => $environment));
$smtp = $em->getRepository('NTIEmailBundle:Smtp')->findOneBy(array("environment" => $environment, "uniqueId" => strtolower($from)));

if (!$smtp) {
if ($this->container->has('nti.logger')) {
$this->container->get('nti.logger')->logError("Unable to find an SMTP configuration for this environment.");
$this->container->get('nti.logger')->logError("Unable to find an SMTP configuration for this environment and {$from}.");
}
return false;
}


// Create a new temporary spool
$hash = md5(uniqid(time()));
$tempSpoolPath = $this->container->getParameter('swiftmailer.spool.default.file.path')."/".$hash."/";
$tempSpoolPath = $smtp->getSpoolDir()."/".$hash."/";
$tempSpool = new \Swift_FileSpool($tempSpoolPath);

/** @var \Swift_Mailer $mailer */
Expand All @@ -393,7 +417,7 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
$files = scandir($tempSpoolPath, SORT_ASC);
if(count($files) <= 0) {
if($this->container->has('nti.logger')){
$this->container->get('nti.logger')->logError("Unable to find file in temporary spool...");
$this->container->get('nti.logger')->logError("Unable to find file in temporary spool with config: {$smtp->getUniqueId()}...");
}
}
$filename = null;
Expand Down Expand Up @@ -423,7 +447,7 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
$from = (is_array($message->getFrom())) ? join(', ', array_keys($message->getFrom())) : $message->getFrom();
$recipients = (is_array($message->getTo())) ? join(', ', array_keys($message->getTo())) : $message->getTo();
$email->setFilename($filename);
$email->setPath($this->container->getParameter('swiftmailer.spool.default.file.path')."/");
$email->setPath($smtp->getSpoolDir()."/");
$email->setMessageFrom($from);
$email->setMessageTo($recipients);
$email->setMessageSubject($message->getSubject());
Expand Down
15 changes: 10 additions & 5 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
"name" : "nti/email-bundle",
"description" : "Symfony NTIEmailBundle",
"type" : "symfony-bundle",
"authors" : [{
"name" : "Benjamin Vison",
"email" : "[email protected]"
}],
"authors" : [
{
"name" : "Benjamin Vison",
"email" : "[email protected]"
},{
"name" : "Enercido Alcantara",
"email" : "[email protected]"
}
],
"keywords" : [
"LogBundle"
"LogBundle", "EmailBundle"
],
"license" : [
"MIT"
Expand Down

0 comments on commit f3e4f02

Please sign in to comment.