2018-09-20 11:39:03 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-09-20 11:39:03 +02:00
|
|
|
|
|
|
|
namespace Notifications;
|
|
|
|
|
2019-08-29 10:25:14 +02:00
|
|
|
use Modules\Emails\Account;
|
2019-12-27 15:56:20 +01:00
|
|
|
use Modules\Emails\Mail;
|
2020-01-03 17:11:15 +01:00
|
|
|
use PHPMailer\PHPMailer\Exception;
|
2021-07-27 10:16:10 +02:00
|
|
|
use PHPMailer\PHPMailer\OAuth;
|
2021-07-27 18:11:54 +02:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2021-07-27 10:16:10 +02:00
|
|
|
use PHPMailer\PHPMailer\SMTP;
|
2018-09-20 11:39:03 +02:00
|
|
|
use Prints;
|
2018-11-23 15:17:52 +01:00
|
|
|
use Uploads;
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
class EmailNotification extends PHPMailer implements NotificationInterface
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2019-08-26 18:02:05 +02:00
|
|
|
protected $mail;
|
2019-11-26 17:03:25 +01:00
|
|
|
protected $directory;
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
protected $infos = [];
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
public function __construct($account = null, $exceptions = null)
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2021-08-05 11:18:54 +02:00
|
|
|
parent::__construct($exceptions);
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->CharSet = 'UTF-8';
|
|
|
|
|
|
|
|
// Configurazione di base
|
2021-07-27 10:16:10 +02:00
|
|
|
$account = $account instanceof Account ? $account : Account::find($account);
|
|
|
|
if (empty($account)) {
|
|
|
|
$account = Account::where('predefined', true)->first();
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preparazione email
|
|
|
|
$this->IsHTML(true);
|
|
|
|
|
2021-07-27 10:16:10 +02:00
|
|
|
if (!empty($account['server'])) {
|
|
|
|
$this->IsSMTP();
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
// Impostazioni di debug
|
2021-07-27 14:49:18 +02:00
|
|
|
$this->SMTPDebug = 2;
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->Debugoutput = function ($str, $level) {
|
|
|
|
$this->infos[] = $str;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Impostazioni dell'host
|
2021-07-27 10:16:10 +02:00
|
|
|
$this->Host = $account['server'];
|
|
|
|
$this->Port = $account['port'];
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
// Impostazioni di autenticazione
|
2021-07-27 10:16:10 +02:00
|
|
|
if (!empty($account['username'])) {
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->SMTPAuth = true;
|
2021-07-27 10:16:10 +02:00
|
|
|
$this->Username = $account['username'];
|
|
|
|
|
|
|
|
// Configurazione OAuth2
|
2021-09-23 17:13:19 +02:00
|
|
|
$oauth2 = $account->oauth2;
|
|
|
|
if (!empty($oauth2)) {
|
2021-07-27 10:16:10 +02:00
|
|
|
$this->AuthType = 'XOAUTH2';
|
|
|
|
$this->setOAuth(
|
|
|
|
new OAuth([
|
|
|
|
'provider' => $oauth2->getProvider(),
|
|
|
|
'refreshToken' => $oauth2->getRefreshToken(),
|
2021-09-23 17:13:19 +02:00
|
|
|
'clientId' => $oauth2->client_id,
|
|
|
|
'clientSecret' => $oauth2->client_secret,
|
2021-07-27 10:16:10 +02:00
|
|
|
'userName' => $account->username,
|
|
|
|
])
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->Password = $account['password'];
|
|
|
|
}
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Impostazioni di sicurezza
|
2021-07-27 10:16:10 +02:00
|
|
|
if (in_array(strtolower($account['encryption']), ['ssl', 'tls'])) {
|
|
|
|
$this->SMTPSecure = strtolower($account['encryption']);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 10:16:10 +02:00
|
|
|
// Disabilitazione verifica host
|
|
|
|
if (!empty($account['ssl_no_verify'])) {
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->SMTPOptions = [
|
|
|
|
'ssl' => [
|
|
|
|
'verify_peer' => false,
|
|
|
|
'verify_peer_name' => false,
|
|
|
|
'allow_self_signed' => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 10:16:10 +02:00
|
|
|
$this->From = $account['from_address'];
|
|
|
|
$this->FromName = $account['from_name'];
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
$this->WordWrap = 78;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:52:58 +02:00
|
|
|
public static function build(Mail $mail, $exceptions = null)
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2019-08-26 18:02:05 +02:00
|
|
|
$result = new self($mail->account->id, $exceptions);
|
|
|
|
|
2020-03-31 19:52:58 +02:00
|
|
|
$result->setMail($mail);
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
return $result;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:52:58 +02:00
|
|
|
public function setMail($mail)
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->mail = $mail;
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
// Registazione della processazione
|
|
|
|
if (!empty($this->mail)) {
|
|
|
|
$this->mail->processing_at = date('Y-m-d H:i:s');
|
|
|
|
$this->mail->save();
|
|
|
|
}
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Destinatari
|
|
|
|
$receivers = $mail->receivers;
|
|
|
|
foreach ($receivers as $receiver) {
|
2019-09-06 17:40:02 +02:00
|
|
|
$this->addReceiver($receiver['address'], $receiver['type']);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Allegati
|
2019-08-29 11:25:13 +02:00
|
|
|
$uploads = $mail->uploads;
|
2019-08-26 18:02:05 +02:00
|
|
|
foreach ($uploads as $upload) {
|
2019-09-19 18:46:07 +02:00
|
|
|
$this->addUpload($upload->id);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Stampe
|
2020-03-31 19:52:58 +02:00
|
|
|
$prints = $mail->prints;
|
2019-08-26 18:02:05 +02:00
|
|
|
foreach ($prints as $print) {
|
2020-03-31 19:52:58 +02:00
|
|
|
$this->addPrint($print['id'], $mail->id_record);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Conferma di lettura
|
|
|
|
if (!empty($mail->read_notify)) {
|
|
|
|
$this->ConfirmReadingTo = $mail->From;
|
2021-08-03 14:21:29 +02:00
|
|
|
$this->AddCustomHeader('X-Confirm-Reading-To: '.$mail->From);
|
|
|
|
$this->AddCustomHeader('Return-Receipt-To: '.$mail->From);
|
|
|
|
$this->AddCustomHeader('Disposition-Notification-To: '.$mail->From);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|
2019-06-04 20:45:40 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Reply To
|
|
|
|
if (!empty($mail->options['reply_to'])) {
|
|
|
|
$this->AddReplyTo($mail->options['reply_to']);
|
2018-09-20 14:41:01 +02:00
|
|
|
}
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
// Oggetto
|
|
|
|
$this->Subject = $mail->subject;
|
|
|
|
|
|
|
|
// Contenuto
|
|
|
|
$this->Body = $mail->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMail()
|
|
|
|
{
|
|
|
|
return $this->mail;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-26 18:02:05 +02:00
|
|
|
* Invia l'email impostata.
|
2018-09-20 11:39:03 +02:00
|
|
|
*
|
2019-08-26 18:02:05 +02:00
|
|
|
* @throws Exception
|
|
|
|
*
|
|
|
|
* @return bool
|
2018-09-20 11:39:03 +02:00
|
|
|
*/
|
2019-08-26 18:02:05 +02:00
|
|
|
public function send()
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2019-08-26 18:02:05 +02:00
|
|
|
if (empty($this->AltBody)) {
|
|
|
|
$this->AltBody = strip_tags($this->Body);
|
|
|
|
}
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
$result = parent::send();
|
2019-12-27 15:56:20 +01:00
|
|
|
} catch (Exception $e) {
|
2019-08-26 18:02:05 +02:00
|
|
|
$result = false;
|
|
|
|
$exception = $e;
|
|
|
|
}
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
// Registazione invio
|
|
|
|
if (!empty($this->mail)) {
|
|
|
|
if ($result) {
|
|
|
|
$this->mail->sent_at = date('Y-m-d H:i:s');
|
|
|
|
} else {
|
|
|
|
$this->mail->failed_at = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:29:59 +01:00
|
|
|
// Salvataggio del numero di tentativi
|
|
|
|
$this->mail->attempt = $this->mail->attempt + 1;
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
$this->mail->save();
|
2019-08-27 15:42:13 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->SmtpClose();
|
|
|
|
|
|
|
|
// Pulizia file generati
|
2019-11-29 17:19:22 +01:00
|
|
|
//delete($this->getTempDirectory());
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
// Segnalazione degli errori
|
|
|
|
if (!$result) {
|
|
|
|
$logger = logger();
|
|
|
|
foreach ($this->infos as $info) {
|
|
|
|
$logger->addRecord(\Monolog\Logger::ERROR, $info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($exception)) {
|
|
|
|
throw $exception;
|
2018-09-25 16:47:44 +02:00
|
|
|
}
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
return $result;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 18:04:49 +02:00
|
|
|
/**
|
|
|
|
* Testa la connessione al server SMTP.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function testSMTP()
|
|
|
|
{
|
|
|
|
if ($this->smtpConnect()) {
|
|
|
|
$this->smtpClose();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-23 15:17:52 +01:00
|
|
|
/**
|
|
|
|
* Aggiunge un allegato del gestionale alla notifica.
|
|
|
|
*
|
|
|
|
* @param string $file_id
|
|
|
|
*/
|
|
|
|
public function addUpload($file_id)
|
|
|
|
{
|
|
|
|
$attachment = database()->fetchOne('SELECT * FROM zz_files WHERE id = '.prepare($file_id));
|
|
|
|
|
2021-06-03 10:49:39 +02:00
|
|
|
$this->addAttachment(base_dir().'/'.Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename'], $attachment['original']);
|
2018-11-23 15:17:52 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:39:03 +02:00
|
|
|
/**
|
|
|
|
* Aggiunge una stampa alla notifica.
|
|
|
|
*
|
|
|
|
* @param string|int $print
|
2018-09-25 16:47:44 +02:00
|
|
|
* @param int $id_record
|
|
|
|
* @param string $name
|
2018-09-20 11:39:03 +02:00
|
|
|
*/
|
|
|
|
public function addPrint($print, $id_record, $name = null)
|
|
|
|
{
|
|
|
|
$print = Prints::get($print);
|
|
|
|
|
2019-11-29 17:19:22 +01:00
|
|
|
$info = Prints::render($print['id'], $id_record, null, true);
|
|
|
|
$name = $name ?: $info['path'];
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-11-29 17:19:22 +01:00
|
|
|
$this->AddStringAttachment($info['pdf'], $name);
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-26 18:02:05 +02:00
|
|
|
* Aggiunge un destinatario.
|
2018-09-20 11:39:03 +02:00
|
|
|
*
|
2019-08-26 18:02:05 +02:00
|
|
|
* @param array $receiver
|
|
|
|
* @param array $type
|
2018-09-20 11:39:03 +02:00
|
|
|
*/
|
2019-08-26 18:02:05 +02:00
|
|
|
public function addReceiver($receiver, $type = null)
|
2018-09-20 11:39:03 +02:00
|
|
|
{
|
2019-08-26 18:02:05 +02:00
|
|
|
$pieces = explode('<', $receiver);
|
|
|
|
$count = count($pieces);
|
|
|
|
|
|
|
|
$name = null;
|
|
|
|
if ($count > 1) {
|
|
|
|
$email = substr(end($pieces), 0, -1);
|
|
|
|
$name = substr($receiver, 0, strpos($receiver, '<'.$email));
|
|
|
|
} else {
|
|
|
|
$email = $receiver;
|
2018-09-20 14:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
if (!empty($email)) {
|
|
|
|
if ($type == 'cc') {
|
|
|
|
$this->AddCC($email, $name);
|
|
|
|
} elseif ($type == 'bcc') {
|
|
|
|
$this->AddBCC($email, $name);
|
|
|
|
} else {
|
|
|
|
$this->AddAddress($email, $name);
|
2018-11-23 15:17:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
2019-11-26 17:03:25 +01:00
|
|
|
|
|
|
|
protected function getTempDirectory()
|
|
|
|
{
|
|
|
|
if (!isset($this->directory)) {
|
2020-09-23 13:36:37 +02:00
|
|
|
$this->directory = base_dir().'/files/notifications/'.rand(0, 999);
|
2019-11-26 17:03:25 +01:00
|
|
|
|
|
|
|
directory($this->directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->directory;
|
|
|
|
}
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|