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