Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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