Skip to content

Commit

Permalink
Implemented the resend email feature and reorganized the way emails a…
Browse files Browse the repository at this point in the history
…re stored.
  • Loading branch information
bvisonl committed Jun 19, 2017
1 parent e623b90 commit d37cbc7
Show file tree
Hide file tree
Showing 13 changed files with 820 additions and 492 deletions.
10 changes: 0 additions & 10 deletions .idea/EmailBundle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 109 additions & 74 deletions .idea/workspace.xml

Large diffs are not rendered by default.

79 changes: 72 additions & 7 deletions Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace NTI\EmailBundle\Command;

use Doctrine\ORM\EntityManager;
use NTI\EmailBundle\Entity\Email;
use NTI\EmailBundle\Entity\Smtp;
use Swift_Spool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CheckCommand extends ContainerAwareCommand
Expand All @@ -22,8 +21,56 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$spoolFolder = $this->getContainer()->getParameter('swiftmailer.spool.default.file.path');
$em = $this->getContainer()->get('doctrine')->getManager();

/** @var Smtp $smtp */
$smtp = $em->getRepository('NTIEmailBundle:Smtp')->findOneBy(array("environment" => $this->getContainer()->getParameter('environment')));

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


$spoolFolder = $this->getContainer()->getParameter('swiftmailer.spool.default.file.path');

// Send Emails
//create an instance of the spool object pointing to the right position in the filesystem
$spool = new \Swift_FileSpool($spoolFolder);

//create a new instance of Swift_SpoolTransport that accept an argument as Swift_FileSpool
$transport = \Swift_SpoolTransport::newInstance($spool);

//now create an instance of the transport you usually use with swiftmailer
//to send real-time email
$realTransport = \Swift_SmtpTransport::newInstance(
$smtp->getHost(),
$smtp->getPort(),
$smtp->getEncryption()
)
->setUsername($smtp->getUser())
->setPassword($smtp->getPassword());

/** @var \Swift_FileSpool $spool */
$spool = $transport->getSpool();
$spool->setMessageLimit(10);
$spool->setTimeLimit(100);

$sent = 0;

try {
$sent = $spool->flushQueue($realTransport);
} catch (\Exception $ex) {
if($this->getContainer()->has('nti.logger')){
$this->getContainer()->get('nti.logger')->logException($ex);
}
}

$output->writeln("Sent ".$sent." emails.");

// Check email statuses
$emails = $em->getRepository('NTIEmailBundle:Email')->findEmailsToCheck();

if(count($emails) <= 0) {
Expand All @@ -34,15 +81,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
/** @var Email $email */
foreach($emails as $email) {

// Update last check date
$email->setLastCheck(new \DateTime());

// Check if it is sending
if(file_exists($spoolFolder."/".$email->getFilename().".sending")) {
$retry = new \DateTime();
$retry->sub(new \DateInterval('PT1M'));
if($email->getLastCheck() < $retry) {
// Retry
if($email->getRetryCount() >= 5) {
$email->setLastCheck(new \DateTime());
$email->setRetryCount(0); // Reset count
$email->setStatus(Email::STATUS_FAILURE);
continue;
} else {
@rename($spoolFolder."/".$email->getFilename().".sending", $spoolFolder."/".$email->getFilename());
$count = $email->getRetryCount() ? $email->getRetryCount() : 1;
$email->setRetryCount($count + 1);
$email->setStatus(Email::STATUS_QUEUE);
continue;
}
}
$email->setStatus(Email::STATUS_SENDING);
continue;
}

// Update last check date
$email->setLastCheck(new \DateTime());


// Check if it's creating, in this case, the mailer didn't get to create the
// file inside the hash folder before the code reaches it, therefore we have to check
// the hash folder so we can finish that process here
Expand Down
45 changes: 45 additions & 0 deletions Command/ResendCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace NTI\EmailBundle\Command;

use NTI\EmailBundle\Entity\Email;
use NTI\EmailBundle\Entity\Smtp;
use Swift_Spool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ResendCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('nti:email:resend')
->setDescription("Resends an email by putting it in the spool folder again and changing the status to QUEUE")
->addArgument("emailId", InputArgument::REQUIRED, "The Email Id to be resent")
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$id = $input->getArgument("emailId");
if(!$id) {
$output->writeln("<error>The Email ID is required.</error>");
return;
}

$em = $this->getContainer()->get('doctrine')->getManager();
$email = $em->getRepository('NTIEmailBundle:Email')->find($id);
if(!$email) {
$output->writeln("<error>The Email was not found.</error>");
return;
}

$smtpService = $this->getContainer()->get('nti.mailer');

$smtpService->resend($email);
$output->writeln("<success>The Email was moved to the queue.</success>");
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/NTIEmailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NTIEmailExtension extends Extension
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

Expand Down
Loading

0 comments on commit d37cbc7

Please sign in to comment.