Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Rev 606 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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