openstamanager/src/Notifications/EmailNotification.php

313 lines
8.6 KiB
PHP
Raw Normal View History

<?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/>.
*/
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;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
2019-08-26 18:02:05 +02:00
class EmailNotification extends PHPMailer implements NotificationInterface
{
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 = [];
2019-08-26 18:02:05 +02:00
public function __construct($account = null, $exceptions = null)
{
2021-08-05 11:18:54 +02:00
parent::__construct($exceptions);
2019-08-26 18:02:05 +02:00
$this->CharSet = 'UTF-8';
// Configurazione di base
$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);
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
$this->Host = $account['server'];
$this->Port = $account['port'];
2019-08-26 18:02:05 +02:00
// Impostazioni di autenticazione
if (!empty($account['username'])) {
2019-08-26 18:02:05 +02:00
$this->SMTPAuth = true;
$this->Username = $account['username'];
// Configurazione OAuth2
$oauth2 = $account->oauth2;
if (!empty($oauth2)) {
$this->AuthType = 'XOAUTH2';
$this->setOAuth(
new OAuth([
'provider' => $oauth2->getProvider(),
'refreshToken' => $oauth2->getRefreshToken(),
'clientId' => $oauth2->client_id,
'clientSecret' => $oauth2->client_secret,
'userName' => $account->username,
])
);
} else {
$this->Password = $account['password'];
}
2019-08-26 18:02:05 +02:00
}
// Impostazioni di sicurezza
if (in_array(strtolower($account['encryption']), ['ssl', 'tls'])) {
$this->SMTPSecure = strtolower($account['encryption']);
2019-08-26 18:02:05 +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,
],
];
}
}
$this->From = $account['from_address'];
$this->FromName = $account['from_name'];
2019-08-26 18:02:05 +02:00
$this->WordWrap = 78;
}
public static function build(Mail $mail, $exceptions = null)
{
2019-08-26 18:02:05 +02:00
$result = new self($mail->account->id, $exceptions);
$result->setMail($mail);
2019-08-26 18:02:05 +02:00
return $result;
}
public function setMail($mail)
{
2019-08-26 18:02:05 +02:00
$this->mail = $mail;
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
$prints = $mail->prints;
2019-08-26 18:02:05 +02:00
foreach ($prints as $print) {
$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 = $this->From;
$this->AddCustomHeader('X-Confirm-Reading-To: '.$this->From);
$this->AddCustomHeader('Return-Receipt-To: '.$this->From);
$this->AddCustomHeader('Disposition-Notification-To: '.$this->From);
2019-08-26 18:02:05 +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;
}
/**
2019-08-26 18:02:05 +02:00
* Invia l'email impostata.
*
2019-08-26 18:02:05 +02:00
* @throws Exception
*
* @return bool
*/
2019-08-26 18:02:05 +02:00
public function send()
{
2019-08-26 18:02:05 +02:00
if (empty($this->AltBody)) {
$this->AltBody = strip_tags($this->Body);
}
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
2024-01-15 15:30:45 +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;
}
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));
2024-01-15 15:30:45 +01: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
}
/**
* Aggiunge una stampa alla notifica.
*
* @param string|int $print
2018-09-25 16:47:44 +02:00
* @param int $id_record
* @param string $name
*/
public function addPrint($print, $id_record, $name = null)
{
2024-01-15 15:30:45 +01:00
$print = \Prints::get($print);
2024-01-15 15:30:45 +01:00
$info = \Prints::render($print['id'], $id_record, null, true);
2019-11-29 17:19:22 +01:00
$name = $name ?: $info['path'];
2019-11-29 17:19:22 +01:00
$this->AddStringAttachment($info['pdf'], $name);
}
/**
2019-08-26 18:02:05 +02:00
* Aggiunge un destinatario.
*
2019-08-26 18:02:05 +02:00
* @param array $receiver
* @param array $type
*/
2019-08-26 18:02:05 +02:00
public function addReceiver($receiver, $type = null)
{
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
}
}
}
2019-11-26 17:03:25 +01:00
protected function getTempDirectory()
{
if (!isset($this->directory)) {
2024-04-08 15:44:33 +02:00
$this->directory = base_dir().'/files/notifications/'.random_int(0, 999);
2019-11-26 17:03:25 +01:00
directory($this->directory);
}
return $this->directory;
}
}