Rev 615 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types = 1);
namespace LeadersLinked\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
// use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\EmailMapper;
use PHPMailer\PHPMailer\PHPMailer;
use LeadersLinked\Model\Email;
use Laminas\Mvc\I18n\Translator;
use LeadersLinked\Cache\CacheInterface;
class ProcessQueueEmailCommand extends Command
{
/**
*
* @var \Laminas\Db\Adapter\AdapterInterface
*/
private $adapter;
/**
*
* @var \LeadersLinked\Cache\CacheInterface
*/
private $cache;
/**
*
* @var \Laminas\Log\LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var \Laminas\Mvc\I18n\Translator
*/
private $translator;
/**
*
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
* @param \LeadersLinked\Cache\CacheInterface $cache
* @param
* \Laminas\Log\LoggerInterface
* @param array $config
* @param \Laminas\Mvc\I18n\Translator $translator
*/
public function __construct($adapter, $cache, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$sandbox = $this->config['leaderslinked.runmode.sandbox'];
if ($sandbox) {
$batch_size = $this->config['leaderslinked.email.sandbox_batch_size'];
$from_address = $this->config['leaderslinked.email.sandbox_from_address'];
$from_name = $this->config['leaderslinked.email.sandbox_from_name'];
$host = $this->config['leaderslinked.email.sandbox_host'];
$port = $this->config['leaderslinked.email.sandbox_port'];
$username = $this->config['leaderslinked.email.sandbox_username'];
$password = $this->config['leaderslinked.email.sandbox_password'];
} else {
$batch_size = $this->config['leaderslinked.email.production_batch_size'];
$from_address = $this->config['leaderslinked.email.production_from_address'];
$from_name = $this->config['leaderslinked.email.production_from_name'];
$host = $this->config['leaderslinked.email.production_host'];
$port = $this->config['leaderslinked.email.production_port'];
$username = $this->config['leaderslinked.email.production_username'];
$password = $this->config['leaderslinked.email.production_password'];
}
echo 'Username : ' . $username . PHP_EOL;
echo 'Password : ' . $password . PHP_EOL;
echo 'Host : ' . $host . PHP_EOL;
echo 'Port : ' . $port . PHP_EOL;
$this->logger->info('Inicio del proceso de la cola de Email');
$emailCompleted = 0;
$emailError = 0;
$emailMapper = EmailMapper::getInstance($this->adapter);
$emails = $emailMapper->fetchBatch($batch_size);
if ($emails) {
foreach ($emails as $email) {
$content = json_decode($email->content, true);
$to_address = $content['to_address'];
$to_name = $content['to_name'];
$cc = $content['cc'];
$bcc = $content['bcc'];
$subject = $content['subject'];
$message = $content['message'];
$encoding = mb_detect_encoding($subject);
if ($encoding != 'UTF-8') {
$subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
}
$encoding = mb_detect_encoding($message);
if ($encoding != 'UTF-8') {
$message = mb_convert_encoding($message, 'UTF-8', $encoding);
}
try{
$phpMailer = new PHPMailer(true);
$phpMailer->SMTPDebug = false;
$phpMailer->isSMTP();
$phpMailer->Host = $host;
$phpMailer->SMTPAuth = true;
$phpMailer->Username = $username;
$phpMailer->Password = $password;
$phpMailer->SMTPSecure = 'tls';
$phpMailer->Port = $port;
$phpMailer->setFrom($from_address, $from_name);
$phpMailer->addAddress($to_address, $to_name);
if ($cc) {
foreach ($cc as $address => $name) {
$phpMailer->addCC($address, $name);
}
}
if ($bcc) {
foreach ($bcc as $address => $name) {
$phpMailer->addBCC($address, $name);
}
}
$phpMailer->IsHTML(true);
$phpMailer->Subject = $subject;
$phpMailer->Body = $message;
$phpMailer->CharSet = 'UTF-8';
$phpMailer->AltBody = $message;
$phpMailer->send();
$emailCompleted ++;
$email->status = Email::STATUS_COMPLETED;
$this->logger->info('Email enviado correctamente: ' . $email->id);
} catch (\Exception $e) {
$this->logger->info('Error al enviar el email: ' . $e->getMessage() . ' - ' . $phpMailer->ErrorInfo);
if ($email->tried == 2) {
$emailError ++;
$email->status = Email::STATUS_ERROR;
$this->logger->info('Email descartado: ' . $email->id);
}
$email->tried ++;
}
try {
$emailMapper->update($email);
} catch (\Exception $e) {
$this->logger->info('Error al actualizar el email: ' . $e->getMessage());
}
}
}
$this->logger->info('Email con Errores descartados: ' . $emailError);
$this->logger->info('Email enviados correctamente:' . $emailCompleted);
$this->logger->info('Fin del proceso de la cola de Email');
return 0;
}
}