Skip to content

Commit

Permalink
Se agrego restricción de tiempo para el intento de envío de correos
Browse files Browse the repository at this point in the history
  • Loading branch information
jogianotti committed Mar 20, 2018
1 parent 6488b48 commit 99b0984
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
44 changes: 44 additions & 0 deletions src/Celsius3/CoreBundle/Entity/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ class Email
*/
private $sent;

/**
* @ORM\Column(name="attempts", type="integer")
*/
private $attempts = 0;

/**
* @ORM\Column(name="error", type="boolean")
*/
private $error = false;

/**
* Get id.
*
Expand Down Expand Up @@ -231,4 +241,38 @@ public function getSent()
{
return $this->sent;
}

/**
* @return mixed
*/
public function getAttempts()
{
return $this->attempts;
}

/**
* Add attempt
*/
public function addAttempt()
{
$this->attempts++;
}

/**
* @return mixed
*/
public function getError()
{
return $this->error;
}

/**
* @param mixed $error
*/
public function setError($error)
{
$this->error = $error;
}


}
49 changes: 31 additions & 18 deletions src/Celsius3/CoreBundle/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,41 @@ public function sendInstanceEmails(Instance $instance, $limit, OutputInterface $

foreach ($emails as $email) {
try {
$from = $instance->get(ConfigurationHelper::CONF__SMTP_USERNAME)->getValue();
if ($logLevel <= 2) {
$output->writeln('Sending mail from ' . $from . ' to ' . $email->getAddress());
$logger->info('Sending mail from ' . $from . ' to ' . $email->getAddress());
if ($email->getAttempts() < 10 || $email->getUpdatedAt()->diff(new \DateTime())->h > 2) {
$from = $instance->get(ConfigurationHelper::CONF__SMTP_USERNAME)->getValue();
if ($logLevel <= 2) {
$output->writeln('Sending mail from ' . $from . ' to ' . $email->getAddress());
$logger->info('Sending mail from ' . $from . ' to ' . $email->getAddress());
}
if ($logLevel === 1) {
$output->writeln('Subject: ' . $email->getSubject());
$logger->info('Instance ' . $instance->getUrl() . ': The SMTP server data are not valid.');
}

$message = \Swift_Message::newInstance()
->setSubject($email->getSubject())
->setFrom($from)
->setTo($email->getAddress())
->setBody($email->getText() . "\n" . $signature, 'text/html')
->addPart($email->getText() . "\n" . $signature, 'text/html');

if ($mailer->send($message)) {
$em->persist($email->setSent(true));
$em->flush($email);
}
}
if ($logLevel === 1) {
$output->writeln('Subject: ' . $email->getSubject());
$logger->info('Instance ' . $instance->getUrl() . ': The SMTP server data are not valid.');
} catch (\Exception $e) {
$email->addAttempt();

$diff = $email->getCreatedAt()->diff(new \DateTime());
$hours = $diff->h + ($diff->days * 24);
if($hours > 48) {
$email->setError(true);
}

$message = \Swift_Message::newInstance()
->setSubject($email->getSubject())
->setFrom($from)
->setTo($email->getAddress())
->setBody($email->getText() . "\n" . $signature, 'text/html')
->addPart($email->getText() . "\n" . $signature, 'text/html');
$this->em->persist($email);
$this->em->flush();

if ($mailer->send($message)) {
$em->persist($email->setSent(true));
$em->flush($email);
}
} catch (\Exception $e) {
$message = "Error al enviar el correo con ID: " . $email->getId();

$logger->error($message);
Expand Down
2 changes: 2 additions & 0 deletions src/Celsius3/CoreBundle/Repository/EmailRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function findNotSentEmailsWithLimit(Instance $instance, $limit)
->setParameter('instance', $instance->getId())
->andWhere('e.sent = :sent')
->setParameter('sent', false)
->andWhere('e.error = :error')
->setParameter('error', false)
->setMaxResults($limit)
;

Expand Down

0 comments on commit 99b0984

Please sign in to comment.