Issues (2050)

extras/residentesEnviarMail.php (3 issues)

1
<?php
2
/*
3
 * Copyright (C) 2021 Joe Nilson <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
require_once 'extras/phpmailer/class.phpmailer.php';
19
require_once 'extras/phpmailer/class.smtp.php';
20
21
require 'plugins/residentes/extras/vendor/autoload.php';
22
23
use \Mailjet\Resources;
24
25
class ResidentesEnviarMail
26
{
27
    public $log;
28
    public $rc;
29
    public $emailConfig;
30
31
    public function __construct()
32
    {
33
        $this->log = new fs_core_log();
34
        $this->rc = new residentes_controller();
35
        $remcfg = new residentes_email_config();
36
        $this->emailConfig = $remcfg->currentConfig();
37
    }
38
39
    public function internalEmailTest()
40
    {
41
42
    }
43
44
    public function internalEmail(&$companyInformation, &$invoice, &$customer, $user, $archivo, $subject)
45
    {
46
        $mail = $companyInformation->new_mail();
47
        $mail->FromName = (is_object($user)) ? $user->get_agente_fullname() : $companyInformation->nombre;
48
        $email = $customer->email;
49
        $this->log->new_message('Enviando factura a: '.$email);
50
        $mail->addAddress($email, $customer->nombre);
51
        $mail->Subject = $subject;
52
        $mail->AltBody = strip_tags(str_replace("<br>", "\n",html_entity_decode($this->emailConfig->emailsubject)));
53
        $this->emailAdditionalInfo($mail, $customer);
54
        $mail->msgHTML(html_entity_decode($this->emailConfig->emailsubject));
55
        $mail->isHTML(true);
56
        $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
57
        $this->emailAdditionalAttachments($mail);
58
        if ($companyInformation->mail_connect($mail) && $mail->send()) {
59
            $invoice->femail = \date('d-m-Y');;
60
            $invoice->save();
61
            $companyInformation->save_mail($mail);
62
        } else {
63
            $this->log->new_error("Error al enviar el email: " . $mail->ErrorInfo);
64
        }
65
        unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
66
    }
67
68
    public function sendgridEmailTest()
69
    {
70
        $email = new \SendGrid\Mail\Mail();
71
        $email->setFrom($this->emailConfig->apisenderemail, $this->emailConfig->apisendername);
72
        $email->setSubject("Sending with SendGrid is Fun");
73
        $email->addTo($this->emailConfig->apisenderemail, $this->emailConfig->apisendername);
74
        $email->addContent("text/plain", "and easy to do anywhere, even with PHP");
75
        $email->addContent(
76
            "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
77
        );
78
        $sendgrid = new \SendGrid($this->emailConfig->apikey);
79
        try {
80
            $response = $sendgrid->send($email);
81
            return $response->statusCode();
82
        } catch (Exception $e) {
83
            return "ERROR";
84
        }
85
    }
86
87
    public function sendgridEmail(&$companyInformation, &$invoice, &$customer, $user, $archivo, $subject)
88
    {
89
        $email = new \SendGrid\Mail\Mail();
90
        $email->setFrom($this->emailConfig->apisenderemail, $this->emailConfig->apisendername);
91
        $email->setSubject($subject);
92
        $email->addTo($customer->email, $customer->nombre);
93
        $email->addContent("text/plain", strip_tags(str_replace("<br>", "\n",html_entity_decode($this->emailConfig->emailsubject))),);
94
        $email->addContent("text/html", html_entity_decode($this->emailConfig->emailsubject));
95
        $file_encoded = base64_encode(file_get_contents('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo));
96
        $email->addAttachment(
97
            $file_encoded,
98
            "application/pdf",
99
            $invoice->numero2.".pdf",
100
            "attachment"
101
        );
102
103
        $sendgrid = new \SendGrid($this->emailConfig->apikey);
104
        try {
105
            $response = $sendgrid->send($email);
106
            return $response->statusCode();
107
        } catch (Exception $e) {
108
            return "ERROR";
109
        }
110
    }
111
112
    public function mailjetEmailTest()
113
    {
114
        $mj = new \Mailjet\Client($this->emailConfig->apikey,$this->emailConfig->apisecret,true,['version' => 'v3.1']);
115
        $body = [
116
            'Messages' => [
117
                [
118
                    'From' => [
119
                        'Email' => $this->emailConfig->apisenderemail,
120
                        'Name' => $this->emailConfig->apisendername
121
                    ],
122
                    'To' => [
123
                        [
124
                            'Email' => $this->emailConfig->apisenderemail,
125
                            'Name' => $this->emailConfig->apisendername
126
                        ]
127
                    ],
128
                    'Subject' => "Greetings from Mailjet.",
129
                    'TextPart' => "My first Mailjet email",
130
                    'HTMLPart' => "<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet</a>!</h3><br />May the delivery force be with you!",
131
                    'CustomID' => "AppGettingStartedTest"
132
                ]
133
            ]
134
        ];
135
        $response = $mj->post(Resources::$Email, ['body' => $body]);
136
        return ($response->success() === 200) ? $response->success() : "ERROR";
0 ignored issues
show
The condition $response->success() === 200 is always false.
Loading history...
137
    }
138
139
    public function mailjetEmail(&$companyInformation, &$invoice, &$customer, $user, $archivo, $subject)
140
    {
141
        $mj = new \Mailjet\Client($this->emailConfig->apikey,$this->emailConfig->apisecret,true,['version' => 'v3.1']);
142
        $body = [
143
            'Messages' => [
144
                [
145
                    'From' => [
146
                        'Email' => $this->emailConfig->apisenderemail,
147
                        'Name' => $this->emailConfig->apisendername
148
                    ],
149
                    'To' => [
150
                        [
151
                            'Email' =>$customer->email,
152
                            'Name' => $customer->nombre
153
                        ]
154
                    ],
155
                    'Subject' => $subject,
156
                    'TextPart' => strip_tags(str_replace("<br>", "\n",html_entity_decode($this->emailConfig->emailsubject))),
157
                    'HTMLPart' => html_entity_decode($this->emailConfig->emailsubject),
158
                    'Attachments' => [
159
                        [
160
                            'ContentType' => 'application/pdf',
161
                            'Filename' => $invoice->numero2.'.pdf',
162
                            'Base64Content' => base64_encode(
163
                                file_get_contents('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo)
164
                            )
165
                        ]
166
                    ]
167
                ]
168
            ]
169
        ];
170
171
        $response = $mj->post(Resources::$Email, ['body' => $body]);
172
        return ($response->success() === 200) ? $response->success() : "ERROR";
0 ignored issues
show
The condition $response->success() === 200 is always false.
Loading history...
173
    }
174
175
    public function invoiceEmail(&$companyInformation, &$invoice, &$customer, $user, $archivo)
176
    {
177
        if (file_exists('tmp/'.FS_TMP_NAME.'enviar/'.$archivo)) {
178
            $subject = fs_fix_html($companyInformation->nombre) .
179
            ' - Su Factura ' . $invoice->codigo . ' ' . $invoice->numero2;
180
            switch($this->emailConfig->tiposervicio) {
181
                case "sendgrid":
182
                    $this->sendgridEmail($companyInformation, $invoice, $customer, $user, $archivo, $subject);
183
                    break;
184
                case "mailjet":
185
                    $this->mailjetEmail($companyInformation, $invoice, $customer, $user, $archivo, $subject);
186
                    break;
187
                case "interno":default:
188
                    $this->internalEmail($companyInformation, $invoice, $customer, $user, $archivo, $subject);
189
                    break;
190
            }
191
//
192
//            $mail = $companyInformation->new_mail();
193
//            $mail->FromName = (is_object($user)) ? $user->get_agente_fullname() : $companyInformation->nombre;
194
//            $email = $customer->email;
195
//            $this->log->new_message('Enviando factura a: '.$email);
196
//            $mail->addAddress($email, $customer->nombre);
197
//            $elSubject = ' - Su Factura ' . $invoice->codigo . ' ' . $invoice->numero2;
198
//            $mail->Subject = fs_fix_html($companyInformation->nombre) . $elSubject;
199
//            $mail->AltBody = plantilla_email(
200
//                'factura',
201
//                $invoice->codigo . ' ' . $invoice->numero2,
202
//                $companyInformation->email_config['mail_firma']
203
//            );
204
//            $this->emailAdditionalInfo($mail, $customer);
205
//            $mail->msgHTML(nl2br($mail->AltBody));
206
//            $mail->isHTML(true);
207
//            $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
208
//            $this->emailAdditionalAttachments($mail);
209
//            if ($companyInformation->mail_connect($mail) && $mail->send()) {
210
//                $invoice->femail = \date('d-m-Y');;
211
//                $invoice->save();
212
//                $companyInformation->save_mail($mail);
213
//            } else {
214
//                $this->log->new_error("Error al enviar el email: " . $mail->ErrorInfo);
215
//            }
216
            unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
217
        } else {
218
            $this->log->new_error('Imposible generar el PDF.');
219
        }
220
    }
221
222
    public function emailAdditionalInfo(&$mail, $customer)
223
    {
224
        if (trim($this->rc->filter_request('email_copia')) !== '') {
225
            if ($this->rc->filter_request('cco') !== null) {
0 ignored issues
show
The condition $this->rc->filter_request('cco') !== null is always true.
Loading history...
226
                $mail->addBCC(
227
                    trim($this->rc->filter_request('email_copia')),
228
                    $customer->nombre
229
                );
230
            } else {
231
                $mail->addCC(
232
                    trim($this->rc->filter_request('email_copia')),
233
                    $customer->nombre
234
                );
235
            }
236
        }
237
    }
238
239
    public function emailAdditionalAttachments(&$mail)
240
    {
241
        if (isset($_FILES['adjunto']) && is_uploaded_file($_FILES['adjunto']['tmp_name'])) {
242
            $mail->addAttachment($_FILES['adjunto']['tmp_name'], $_FILES['adjunto']['name']);
243
        }
244
    }
245
246
    public function accountStatusEmail(&$empresa, &$customer, $user, $archivo)
247
    {
248
        $mail = $empresa->new_mail();
249
        $mail->FromName = (is_object($user)) ? $user->get_agente_fullname() : $empresa->nombre;
250
        $email = (trim($this->rc->filter_request('email')) !== '')
251
            ? $this->rc->filter_request('email')
252
            : $customer->email;
253
        $this->log->new_message('Enviando Estado de cuenta a: '.$email);
254
        $mail->addAddress($email, $customer->nombre);
255
        $elSubject = ': Su Estado de cuenta al '. \date('d-m-Y');
256
        $mail->Subject = fs_fix_html($empresa->nombre) . $elSubject;
257
        $mail->AltBody = strip_tags($_POST['mensaje']);
258
        $this->emailAdditionalAttachments($mail);
259
        $mail->msgHTML(nl2br($mail->AltBody));
260
        $mail->isHTML(true);
261
        $mail->addAttachment('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
262
        $this->emailAdditionalAttachments($mail);
263
        if ($empresa->mail_connect($mail) && $mail->send()) {
264
            $empresa->save_mail($mail);
265
        } else {
266
            $this->log->new_error("Error al enviar el email: " . $mail->ErrorInfo);
267
        }
268
        unlink('tmp/' . FS_TMP_NAME . 'enviar/' . $archivo);
269
    }
270
}