Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Rev 606 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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;
345 www 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
{
345 www 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
345 www 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
 
345 www 70
    protected function execute(InputInterface $input, OutputInterface $output): int
1 efrain 71
    {
72
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
345 www 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'];
1 efrain 81
        } else {
345 www 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'];
1 efrain 89
        }
90
 
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
 
96
        $output->writeln('Inicio del proceso de la cola de Email');
97
 
98
        $emailCompleted = 0;
99
        $emailError = 0;
345 www 100
 
1 efrain 101
        $emailMapper = EmailMapper::getInstance($this->adapter);
102
        $emails = $emailMapper->fetchBatch($batch_size);
103
 
345 www 104
        if ($emails) {
105
            foreach ($emails as $email) {
1 efrain 106
                $content = json_decode($email->content, true);
345 www 107
 
1 efrain 108
                $to_address = $content['to_address'];
345 www 109
                $to_name = $content['to_name'];
110
                $cc = $content['cc'];
111
                $bcc = $content['bcc'];
112
                $subject = $content['subject'];
113
                $message = $content['message'];
1 efrain 114
 
345 www 115
                $encoding = mb_detect_encoding($subject);
116
                if ($encoding != 'UTF-8') {
1 efrain 117
                    $subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
118
                }
345 www 119
 
1 efrain 120
                $encoding = mb_detect_encoding($message);
345 www 121
                if ($encoding != 'UTF-8') {
1 efrain 122
                    $message = mb_convert_encoding($message, 'UTF-8', $encoding);
123
                }
345 www 124
 
1 efrain 125
                $phpMailer = new PHPMailer();
126
                $phpMailer->isSMTP();
345 www 127
 
1 efrain 128
                $phpMailer->addAddress($to_address, $to_name);
345 www 129
                if ($cc) {
130
                    foreach ($cc as $address => $name) {
1 efrain 131
                        $phpMailer->addCC($address, $name);
132
                    }
133
                }
345 www 134
                if ($bcc) {
135
                    foreach ($bcc as $address => $name) {
1 efrain 136
                        $phpMailer->addBCC($address, $name);
137
                    }
138
                }
345 www 139
 
1 efrain 140
                $phpMailer->setFrom($from_address, $from_name);
345 www 141
                $phpMailer->SMTPDebug = false;
142
                $phpMailer->Host = $host;
143
                $phpMailer->Port = $port;
1 efrain 144
                $phpMailer->IsHTML(true);
345 www 145
                $phpMailer->SMTPAuth = true;
146
                $phpMailer->SMTPSecure = 'tls';
147
                $phpMailer->SMTPAuth = true;
148
                $phpMailer->Username = $username;
149
                $phpMailer->Password = $password;
150
                $phpMailer->Subject = $subject;
151
                $phpMailer->Body = $message;
152
                $phpMailer->AltBody = $message;
153
                $phpMailer->CharSet = 'UTF-8';
154
 
1 efrain 155
                $result = $phpMailer->send();
345 www 156
 
157
                if ($result) {
158
                    $emailCompleted ++;
1 efrain 159
                    $email->status = Email::STATUS_COMPLETED;
160
                } else {
345 www 161
                    if ($email->tried == 2) {
162
                        $emailError ++;
1 efrain 163
                        $email->status = Email::STATUS_ERROR;
164
                    }
345 www 165
                    $email->tried ++;
1 efrain 166
                }
167
                $emailMapper->update($email);
168
            }
169
        }
345 www 170
 
1 efrain 171
        $output->writeln('Email con Errores descartados: ' . $emailError);
345 www 172
        $output->writeln('Email enviados correctamente:' . $emailCompleted);
173
 
1 efrain 174
        $output->writeln('Fin del proceso de la cola de Email');
345 www 175
 
1 efrain 176
        return 0;
177
    }
178
}