Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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