Proyectos de Subversion LeadersLinked - Services

Rev

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

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