Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | | Comparar con el anterior | Ultima modificación | Ver Log |

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