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
1979 efrain 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
 
1979 efrain 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 ProcessQueueUserDeletedCommand extends Command
20
{
21
    /**
22
     *
23
     * @var AdapterInterface
24
     */
25
    private $adapter;
6849 efrain 26
 
1979 efrain 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
 
1979 efrain 45
     * @param LoggerInterface $logger
46
     * @param array $config
47
     */
6849 efrain 48
     public function __construct($adapter, $logger, $config)
1979 efrain 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
 
111
 
112
                $phpMailer = new PHPMailer();
113
                $phpMailer->isSMTP();
114
 
115
                $phpMailer->addAddress($to_address, $to_name);
116
                if($cc) {
117
                    foreach($cc as $address => $name) {
118
                        $phpMailer->addCC($address, $name);
119
                    }
120
                }
121
                if($bcc) {
122
                    foreach($bcc as $address => $name) {
123
                        $phpMailer->addBCC($address, $name);
124
                    }
125
                }
126
 
127
                $phpMailer->setFrom($from_address, $from_name);
128
                $phpMailer->SMTPDebug    = false;
129
                $phpMailer->Host         = $host;
130
                $phpMailer->Port         = $port;
131
                $phpMailer->IsHTML(true);
132
                $phpMailer->SMTPAuth    = true;
133
                $phpMailer->SMTPSecure   = 'tls';
134
                $phpMailer->SMTPAuth     = true;
135
                $phpMailer->Username     = $username;
136
                $phpMailer->Password     = $password;
137
                $phpMailer->Subject      = $subject;
138
                $phpMailer->Body         = $message;
139
                $phpMailer->AltBody      = $message;
140
 
141
                /*
142
                $mail->SMTPOptions = [
143
                    'ssl' => [
144
                        'verify_peer' => false,
145
                        'verify_peer_name' => false,
146
                        'allow_self_signed' => true
147
                    ]
148
                ];
149
                */
150
 
151
                //print_r($phpMailer);
152
 
153
                $result = $phpMailer->send();
154
 
155
                if($result) {
156
                    $emailCompleted++;
157
                    $email->status = Email::STATUS_COMPLETED;
158
                } else {
159
                    if($email->tried == 2) {
160
                        $emailError++;
161
                        $email->status = Email::STATUS_ERROR;
162
                    }
163
                    $email->tried++;
164
 
165
                }
166
                $emailMapper->update($email);
167
 
168
 
169
            }
170
        }
171
 
172
        $output->writeln('Email con Errores descartados: ' . $emailError);
173
        $output->writeln('Email enviados correctamente:'  . $emailCompleted);
174
 
175
        $output->writeln('Fin del proceso de la cola de Email');
176
 
177
        return 0;
178
    }
179
 
180
 
181
 
182
}