Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 345 Rev 606
Línea 65... Línea 65...
65
        $this->translator = $translator;
65
        $this->translator = $translator;
Línea 66... Línea 66...
66
 
66
 
67
        parent::__construct();
67
        parent::__construct();
Línea 68... Línea 68...
68
    }
68
    }
69
 
69
 
70
    protected function execute(InputInterface $input, OutputInterface $output): int
-
 
71
    {
-
 
72
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
-
 
73
        if ($sandbox) {
-
 
74
            $batch_size = $this->config['leaderslinked.email.sandbox_batch_size'];
-
 
75
            $from_address = $this->config['leaderslinked.email.sandbox_from_address'];
-
 
76
            $from_name = $this->config['leaderslinked.email.sandbox_from_name'];
-
 
77
            $host = $this->config['leaderslinked.email.sandbox_host'];
-
 
78
            $port = $this->config['leaderslinked.email.sandbox_port'];
-
 
79
            $username = $this->config['leaderslinked.email.sandbox_username'];
-
 
80
            $password = $this->config['leaderslinked.email.sandbox_password'];
-
 
81
        } else {
-
 
82
            $batch_size = $this->config['leaderslinked.email.production_batch_size'];
-
 
83
            $from_address = $this->config['leaderslinked.email.production_from_address'];
-
 
84
            $from_name = $this->config['leaderslinked.email.production_from_name'];
-
 
85
            $host = $this->config['leaderslinked.email.production_host'];
-
 
86
            $port = $this->config['leaderslinked.email.production_port'];
-
 
87
            $username = $this->config['leaderslinked.email.production_username'];
-
 
88
            $password = $this->config['leaderslinked.email.production_password'];
-
 
89
        }
-
 
90
 
-
 
91
        echo 'Username : ' . $username . PHP_EOL;
-
 
92
        echo 'Password : ' . $password . PHP_EOL;
-
 
93
        echo 'Host : ' . $host . PHP_EOL;
-
 
94
        echo 'Port : ' . $port . PHP_EOL;
70
    protected function execute($input, $output)
Línea 95... Línea 71...
95
 
71
    {
96
        $output->writeln('Inicio del proceso de la cola de Email');
72
        $output->writeln('Inicio del proceso de la cola de Email');
Línea 97... Línea 73...
97
 
73
 
98
        $emailCompleted = 0;
74
        $emailConfig = $this->getEmailConfig();
Línea 99... Línea -...
99
        $emailError = 0;
-
 
100
 
-
 
101
        $emailMapper = EmailMapper::getInstance($this->adapter);
75
        $this->logEmailConfig($emailConfig);
102
        $emails = $emailMapper->fetchBatch($batch_size);
-
 
103
 
-
 
104
        if ($emails) {
-
 
105
            foreach ($emails as $email) {
-
 
106
                $content = json_decode($email->content, true);
-
 
107
 
-
 
108
                $to_address = $content['to_address'];
-
 
109
                $to_name = $content['to_name'];
-
 
110
                $cc = $content['cc'];
-
 
111
                $bcc = $content['bcc'];
-
 
112
                $subject = $content['subject'];
-
 
113
                $message = $content['message'];
-
 
Línea 114... Línea 76...
114
 
76
 
115
                $encoding = mb_detect_encoding($subject);
77
        $emailMapper = EmailMapper::getInstance($this->adapter);
116
                if ($encoding != 'UTF-8') {
78
        $emails = $emailMapper->fetchBatch($emailConfig['batch_size']);
-
 
79
 
117
                    $subject = mb_convert_encoding($subject, 'UTF-8', $encoding);
80
        $stats = $this->processEmails($emails, $emailConfig, $emailMapper);
-
 
81
 
Línea -... Línea 82...
-
 
82
        $output->writeln('Email con Errores descartados: ' . $stats['error']);
-
 
83
        $output->writeln('Email enviados correctamente:' . $stats['completed']);
118
                }
84
        $output->writeln('Fin del proceso de la cola de Email');
119
 
85
 
Línea -... Línea 86...
-
 
86
        return 0;
-
 
87
    }
-
 
88
 
120
                $encoding = mb_detect_encoding($message);
89
    private function getEmailConfig(): array
121
                if ($encoding != 'UTF-8') {
90
    {
122
                    $message = mb_convert_encoding($message, 'UTF-8', $encoding);
91
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
123
                }
92
        $prefix = $sandbox ? 'sandbox' : 'production';
-
 
93
 
124
 
94
        return [
125
                $phpMailer = new PHPMailer();
95
            'batch_size' => $this->config["leaderslinked.email.{$prefix}_batch_size"],
-
 
96
            'from_address' => $this->config["leaderslinked.email.{$prefix}_from_address"],
126
                $phpMailer->isSMTP();
97
            'from_name' => $this->config["leaderslinked.email.{$prefix}_from_name"],
-
 
98
            'host' => $this->config["leaderslinked.email.{$prefix}_host"],
127
 
99
            'port' => $this->config["leaderslinked.email.{$prefix}_port"],
128
                $phpMailer->addAddress($to_address, $to_name);
100
            'username' => $this->config["leaderslinked.email.{$prefix}_username"],
-
 
101
            'password' => $this->config["leaderslinked.email.{$prefix}_password"]
129
                if ($cc) {
102
        ];
130
                    foreach ($cc as $address => $name) {
103
    }
Línea 131... Línea 104...
131
                        $phpMailer->addCC($address, $name);
104
 
-
 
105
    private function logEmailConfig($config)
132
                    }
106
    {
-
 
107
        echo 'Username : ' . $config['username'] . PHP_EOL;
133
                }
108
        echo 'Password : ' . $config['password'] . PHP_EOL;
134
                if ($bcc) {
109
        echo 'Host : ' . $config['host'] . PHP_EOL;
135
                    foreach ($bcc as $address => $name) {
110
        echo 'Port : ' . $config['port'] . PHP_EOL;
-
 
111
    }
136
                        $phpMailer->addBCC($address, $name);
112
 
137
                    }
-
 
138
                }
113
    private function processEmails($emails, $config, $emailMapper)
139
 
-
 
140
                $phpMailer->setFrom($from_address, $from_name);
-
 
141
                $phpMailer->SMTPDebug = false;
-
 
142
                $phpMailer->Host = $host;
-
 
143
                $phpMailer->Port = $port;
-
 
144
                $phpMailer->IsHTML(true);
-
 
145
                $phpMailer->SMTPAuth = true;
-
 
146
                $phpMailer->SMTPSecure = 'tls';
114
    {
147
                $phpMailer->SMTPAuth = true;
115
        $stats = ['completed' => 0, 'error' => 0];
148
                $phpMailer->Username = $username;
116
 
149
                $phpMailer->Password = $password;
117
        if (!$emails) {
150
                $phpMailer->Subject = $subject;
118
            return $stats;
151
                $phpMailer->Body = $message;
119
        }
152
                $phpMailer->AltBody = $message;
120
 
153
                $phpMailer->CharSet = 'UTF-8';
121
        foreach ($emails as $email) {
154
 
122
            $content = json_decode($email->content, true);
155
                $result = $phpMailer->send();
-
 
156
 
-
 
157
                if ($result) {
123
            $result = $this->sendEmail($content, $config);
158
                    $emailCompleted ++;
124
 
159
                    $email->status = Email::STATUS_COMPLETED;
125
            if ($result) {
-
 
126
                $stats['completed']++;
160
                } else {
127
                $email->status = Email::STATUS_COMPLETED;
Línea 161... Línea 128...
161
                    if ($email->tried == 2) {
128
            } else {
162
                        $emailError ++;
-
 
-
 
129
                if ($email->tried == 2) {
Línea -... Línea 130...
-
 
130
                    $stats['error']++;
-
 
131
                    $email->status = Email::STATUS_ERROR;
-
 
132
                }
-
 
133
                $email->tried++;
-
 
134
            }
-
 
135
            $emailMapper->update($email);
-
 
136
        }
-
 
137
 
-
 
138
        return $stats;
-
 
139
    }
-
 
140
 
163
                        $email->status = Email::STATUS_ERROR;
141
    private function sendEmail($content, $config)
-
 
142
    {
-
 
143
        $to_address = $content['to_address'];
-
 
144
        $to_name = $content['to_name'];
-
 
145
        $cc = $content['cc'];
-
 
146
        $bcc = $content['bcc'];
-
 
147
        $subject = $this->ensureUtf8($content['subject']);
Línea -... Línea 148...
-
 
148
        $message = $this->ensureUtf8($content['message']);
-
 
149
 
-
 
150
        $phpMailer = new PHPMailer();
-
 
151
        $phpMailer->isSMTP();
-
 
152
        $phpMailer->addAddress($to_address, $to_name);
-
 
153
 
-
 
154
        if ($cc) {
-
 
155
            foreach ($cc as $address => $name) {
-
 
156
                $phpMailer->addCC($address, $name);
-
 
157
            }
-
 
158
        }
-
 
159
 
-
 
160
        if ($bcc) {
-
 
161
            foreach ($bcc as $address => $name) {
-
 
162
                $phpMailer->addBCC($address, $name);
-
 
163
            }
-
 
164
        }
-
 
165
 
-
 
166
        $phpMailer->setFrom($config['from_address'], $config['from_name']);
-
 
167
        $phpMailer->SMTPDebug = 2;
-
 
168
        $phpMailer->Debugoutput = function($str, $level) {
-
 
169
            $this->logger->debug("PHPMailer: $str");
-
 
170
        };
164
                    }
171
        $phpMailer->Host = $config['host'];
-
 
172
        $phpMailer->Port = $config['port'];
-
 
173
        $phpMailer->IsHTML(true);
-
 
174
        $phpMailer->SMTPAuth = true;
-
 
175
        $phpMailer->SMTPSecure = 'tls';
-
 
176
        $phpMailer->Username = $config['username'];
-
 
177
        $phpMailer->Password = $config['password'];
-
 
178
        $phpMailer->Subject = $subject;
-
 
179
        $phpMailer->Body = $message;
-
 
180
        $phpMailer->AltBody = $message;
-
 
181
        $phpMailer->CharSet = 'UTF-8';
-
 
182
 
165
                    $email->tried ++;
183
        try {
166
                }
184
            return $phpMailer->send();
167
                $emailMapper->update($email);
185
        } catch (\Exception $e) {