| 1 |
efrain |
1 |
<?php
|
| 617 |
ariadna |
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
| 1 |
efrain |
5 |
namespace LeadersLinked\Command;
|
|
|
6 |
|
|
|
7 |
use Symfony\Component\Console\Command\Command;
|
|
|
8 |
use Symfony\Component\Console\Input\InputInterface;
|
| 614 |
stevensc |
9 |
// use Symfony\Component\Console\Input\InputOption;
|
| 1 |
efrain |
10 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
11 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
12 |
use Laminas\Log\LoggerInterface;
|
|
|
13 |
use LeadersLinked\Mapper\EmailMapper;
|
|
|
14 |
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
15 |
use LeadersLinked\Model\Email;
|
|
|
16 |
use Laminas\Mvc\I18n\Translator;
|
|
|
17 |
use LeadersLinked\Cache\CacheInterface;
|
|
|
18 |
|
|
|
19 |
class ProcessQueueEmailCommand extends Command
|
|
|
20 |
{
|
| 614 |
stevensc |
21 |
|
| 1 |
efrain |
22 |
/**
|
|
|
23 |
*
|
|
|
24 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
25 |
*/
|
|
|
26 |
private $adapter;
|
| 345 |
www |
27 |
|
| 1 |
efrain |
28 |
/**
|
|
|
29 |
*
|
|
|
30 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
31 |
*/
|
|
|
32 |
private $cache;
|
| 345 |
www |
33 |
|
| 1 |
efrain |
34 |
/**
|
|
|
35 |
*
|
|
|
36 |
* @var \Laminas\Log\LoggerInterface
|
|
|
37 |
*/
|
|
|
38 |
private $logger;
|
| 345 |
www |
39 |
|
| 1 |
efrain |
40 |
/**
|
|
|
41 |
*
|
|
|
42 |
* @var array
|
|
|
43 |
*/
|
|
|
44 |
private $config;
|
| 345 |
www |
45 |
|
| 1 |
efrain |
46 |
/**
|
|
|
47 |
*
|
|
|
48 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
49 |
*/
|
|
|
50 |
private $translator;
|
| 345 |
www |
51 |
|
| 1 |
efrain |
52 |
/**
|
|
|
53 |
*
|
|
|
54 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
55 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
| 614 |
stevensc |
56 |
* @param
|
|
|
57 |
* \Laminas\Log\LoggerInterface
|
| 1 |
efrain |
58 |
* @param array $config
|
|
|
59 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
60 |
*/
|
|
|
61 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
62 |
{
|
| 345 |
www |
63 |
$this->adapter = $adapter;
|
|
|
64 |
$this->cache = $cache;
|
|
|
65 |
$this->logger = $logger;
|
|
|
66 |
$this->config = $config;
|
|
|
67 |
$this->translator = $translator;
|
|
|
68 |
|
| 1 |
efrain |
69 |
parent::__construct();
|
|
|
70 |
}
|
|
|
71 |
|
| 614 |
stevensc |
72 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
| 1 |
efrain |
73 |
{
|
| 609 |
stevensc |
74 |
$sandbox = $this->config['leaderslinked.runmode.sandbox'];
|
|
|
75 |
if ($sandbox) {
|
|
|
76 |
$batch_size = $this->config['leaderslinked.email.sandbox_batch_size'];
|
|
|
77 |
$from_address = $this->config['leaderslinked.email.sandbox_from_address'];
|
|
|
78 |
$from_name = $this->config['leaderslinked.email.sandbox_from_name'];
|
|
|
79 |
$host = $this->config['leaderslinked.email.sandbox_host'];
|
|
|
80 |
$port = $this->config['leaderslinked.email.sandbox_port'];
|
|
|
81 |
$username = $this->config['leaderslinked.email.sandbox_username'];
|
|
|
82 |
$password = $this->config['leaderslinked.email.sandbox_password'];
|
|
|
83 |
} else {
|
|
|
84 |
$batch_size = $this->config['leaderslinked.email.production_batch_size'];
|
|
|
85 |
$from_address = $this->config['leaderslinked.email.production_from_address'];
|
|
|
86 |
$from_name = $this->config['leaderslinked.email.production_from_name'];
|
|
|
87 |
$host = $this->config['leaderslinked.email.production_host'];
|
|
|
88 |
$port = $this->config['leaderslinked.email.production_port'];
|
|
|
89 |
$username = $this->config['leaderslinked.email.production_username'];
|
|
|
90 |
$password = $this->config['leaderslinked.email.production_password'];
|
|
|
91 |
}
|
| 1 |
efrain |
92 |
|
| 616 |
ariadna |
93 |
echo 'Username : ' . $username . PHP_EOL;
|
|
|
94 |
echo 'Password : ' . $password . PHP_EOL;
|
|
|
95 |
echo 'Host : ' . $host . PHP_EOL;
|
|
|
96 |
echo 'Port : ' . $port . PHP_EOL;
|
|
|
97 |
|
| 609 |
stevensc |
98 |
$this->logger->info('Inicio del proceso de la cola de Email');
|
| 1 |
efrain |
99 |
|
| 609 |
stevensc |
100 |
$emailCompleted = 0;
|
| 614 |
stevensc |
101 |
$emailError = 0;
|
| 345 |
www |
102 |
|
| 609 |
stevensc |
103 |
$emailMapper = EmailMapper::getInstance($this->adapter);
|
|
|
104 |
$emails = $emailMapper->fetchBatch($batch_size);
|
| 1 |
efrain |
105 |
|
| 614 |
stevensc |
106 |
if ($emails) {
|
|
|
107 |
foreach ($emails as $email) {
|
| 609 |
stevensc |
108 |
$content = json_decode($email->content, true);
|
| 345 |
www |
109 |
|
| 609 |
stevensc |
110 |
$to_address = $content['to_address'];
|
| 614 |
stevensc |
111 |
$to_name = $content['to_name'];
|
|
|
112 |
$cc = $content['cc'];
|
|
|
113 |
$bcc = $content['bcc'];
|
| 609 |
stevensc |
114 |
$subject = $content['subject'];
|
|
|
115 |
$message = $content['message'];
|
| 345 |
www |
116 |
|
| 614 |
stevensc |
117 |
$encoding = mb_detect_encoding($subject);
|
|
|
118 |
if ($encoding != 'UTF-8') {
|
|
|
119 |
$subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
|
|
|
120 |
}
|
| 345 |
www |
121 |
|
| 614 |
stevensc |
122 |
$encoding = mb_detect_encoding($message);
|
|
|
123 |
if ($encoding != 'UTF-8') {
|
|
|
124 |
$message = mb_convert_encoding($message, 'UTF-8', $encoding);
|
|
|
125 |
}
|
|
|
126 |
|
| 617 |
ariadna |
127 |
try {
|
|
|
128 |
$phpMailer = new PHPMailer(true);
|
| 614 |
stevensc |
129 |
|
| 617 |
ariadna |
130 |
$phpMailer->SMTPDebug = false;
|
|
|
131 |
$phpMailer->isSMTP();
|
|
|
132 |
$phpMailer->Host = $host;
|
|
|
133 |
$phpMailer->SMTPAuth = true;
|
|
|
134 |
$phpMailer->Username = $username;
|
|
|
135 |
$phpMailer->Password = $password;
|
|
|
136 |
$phpMailer->SMTPSecure = 'tls';
|
|
|
137 |
$phpMailer->Port = $port;
|
|
|
138 |
|
|
|
139 |
$phpMailer->setFrom($from_address, $from_name);
|
|
|
140 |
$phpMailer->addAddress($to_address, $to_name);
|
|
|
141 |
|
|
|
142 |
if ($cc) {
|
|
|
143 |
foreach ($cc as $address => $name) {
|
|
|
144 |
$phpMailer->addCC($address, $name);
|
|
|
145 |
}
|
| 614 |
stevensc |
146 |
}
|
| 617 |
ariadna |
147 |
if ($bcc) {
|
|
|
148 |
foreach ($bcc as $address => $name) {
|
|
|
149 |
$phpMailer->addBCC($address, $name);
|
|
|
150 |
}
|
| 614 |
stevensc |
151 |
}
|
| 606 |
stevensc |
152 |
|
| 617 |
ariadna |
153 |
$phpMailer->IsHTML(true);
|
|
|
154 |
$phpMailer->Subject = $subject;
|
|
|
155 |
$phpMailer->Body = $message;
|
|
|
156 |
$phpMailer->CharSet = 'UTF-8';
|
|
|
157 |
$phpMailer->AltBody = $message;
|
| 345 |
www |
158 |
|
| 617 |
ariadna |
159 |
$phpMailer->send();
|
|
|
160 |
|
|
|
161 |
$emailCompleted++;
|
|
|
162 |
$email->status = Email::STATUS_COMPLETED;
|
|
|
163 |
$this->logger->info('Email enviado correctamente: ' . $email->id);
|
| 614 |
stevensc |
164 |
} catch (\Exception $e) {
|
| 617 |
ariadna |
165 |
$this->logger->info('Error al enviar el email: ' . $e->getMessage() . ' - ' . $phpMailer->ErrorInfo);
|
| 614 |
stevensc |
166 |
if ($email->tried == 2) {
|
| 617 |
ariadna |
167 |
$emailError++;
|
| 614 |
stevensc |
168 |
$email->status = Email::STATUS_ERROR;
|
|
|
169 |
$this->logger->info('Email descartado: ' . $email->id);
|
|
|
170 |
}
|
| 617 |
ariadna |
171 |
$email->tried++;
|
| 609 |
stevensc |
172 |
}
|
| 611 |
stevensc |
173 |
|
| 613 |
stevensc |
174 |
try {
|
|
|
175 |
$emailMapper->update($email);
|
| 614 |
stevensc |
176 |
} catch (\Exception $e) {
|
|
|
177 |
$this->logger->info('Error al actualizar el email: ' . $e->getMessage());
|
| 613 |
stevensc |
178 |
}
|
| 606 |
stevensc |
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
| 609 |
stevensc |
182 |
$this->logger->info('Email con Errores descartados: ' . $emailError);
|
| 614 |
stevensc |
183 |
$this->logger->info('Email enviados correctamente:' . $emailCompleted);
|
| 609 |
stevensc |
184 |
$this->logger->info('Fin del proceso de la cola de Email');
|
| 606 |
stevensc |
185 |
|
| 614 |
stevensc |
186 |
return 0;
|
| 1 |
efrain |
187 |
}
|
| 617 |
ariadna |
188 |
}
|