2018-09-20 11:39:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Notifications;
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
use Models\MailAccount;
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
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;
|
|
|
|
protected $attachments = [];
|
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
|
|
|
{
|
2019-08-26 18:02:05 +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
|
|
|
|
$config = MailAccount::find($account);
|
|
|
|
if (empty($config)) {
|
|
|
|
$config = MailAccount::where('predefined', true)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preparazione email
|
|
|
|
$this->IsHTML(true);
|
|
|
|
|
|
|
|
if (!empty($config['server'])) {
|
|
|
|
$this->IsSMTP(true);
|
|
|
|
|
|
|
|
// Impostazioni di debug
|
|
|
|
$this->SMTPDebug = \App::debug() ? 2 : 0;
|
|
|
|
$this->Debugoutput = function ($str, $level) {
|
|
|
|
$this->infos[] = $str;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Impostazioni dell'host
|
|
|
|
$this->Host = $config['server'];
|
|
|
|
$this->Port = $config['port'];
|
|
|
|
|
|
|
|
// Impostazioni di autenticazione
|
|
|
|
if (!empty($config['username'])) {
|
|
|
|
$this->SMTPAuth = true;
|
|
|
|
$this->Username = $config['username'];
|
|
|
|
$this->Password = $config['password'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Impostazioni di sicurezza
|
|
|
|
if (in_array(strtolower($config['encryption']), ['ssl', 'tls'])) {
|
|
|
|
$this->SMTPSecure = strtolower($config['encryption']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($config['ssl_no_verify'])) {
|
|
|
|
$this->SMTPOptions = [
|
|
|
|
'ssl' => [
|
|
|
|
'verify_peer' => false,
|
|
|
|
'verify_peer_name' => false,
|
|
|
|
'allow_self_signed' => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->From = $config['from_address'];
|
|
|
|
$this->FromName = $config['from_name'];
|
|
|
|
|
|
|
|
$this->WordWrap = 78;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
public static function build(\Models\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);
|
|
|
|
|
|
|
|
$result->setMail($mail);
|
|
|
|
|
|
|
|
return $result;
|
2018-09-20 11:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-26 18:02:05 +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-26 18:02:05 +02:00
|
|
|
// Destinatari
|
|
|
|
$receivers = $mail->receivers;
|
|
|
|
foreach ($receivers as $receiver) {
|
|
|
|
$this->addReceiver($receiver['email'], $receiver['type']);
|
|
|
|
}
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Allegati
|
|
|
|
$uploads = $mail->attachments;
|
|
|
|
foreach ($uploads as $upload) {
|
|
|
|
$this->addUpload($upload);
|
|
|
|
}
|
2018-09-20 14:41:01 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
// Stampe
|
|
|
|
$prints = $mail->prints;
|
|
|
|
foreach ($prints as $print) {
|
|
|
|
$this->addPrint($print['id'], $mail->id_record, $print['name']);
|
|
|
|
}
|
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;
|
|
|
|
}
|
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();
|
|
|
|
} catch (PHPMailer\PHPMailer\Exception $e) {
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mail->save();
|
|
|
|
}
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
$this->SmtpClose();
|
|
|
|
|
|
|
|
// Pulizia file generati
|
|
|
|
delete(DOCROOT.'/files/notifications/');
|
|
|
|
|
|
|
|
// 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-09-20 11:39:03 +02:00
|
|
|
/**
|
|
|
|
* Restituisce gli allegati della notifica.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttachments()
|
|
|
|
{
|
|
|
|
return $this->attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imposta gli allegati della notifica.
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
*/
|
|
|
|
public function setAttachments(array $values)
|
|
|
|
{
|
|
|
|
$this->attachments = [];
|
|
|
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$path = is_array($value) ? $value['path'] : $value;
|
|
|
|
$name = is_array($value) ? $value['name'] : null;
|
|
|
|
$this->addAttachment($path, $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
$this->addAttachment(DOCROOT.'/'.Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename']);
|
|
|
|
|
|
|
|
$this->logs['attachments'][] = $attachment['id'];
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (empty($name)) {
|
|
|
|
$name = $print['title'].'.pdf';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utilizzo di una cartella particolare per il salvataggio temporaneo degli allegati
|
2019-08-26 18:02:05 +02:00
|
|
|
$path = DOCROOT.'/files/notifications/'.rand(0, 999);
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-07-10 11:53:35 +02:00
|
|
|
$info = Prints::render($print['id'], $id_record, $path);
|
|
|
|
$name = $name ?: $info['name'];
|
2018-09-20 11:39:03 +02:00
|
|
|
|
2019-07-10 11:53:35 +02:00
|
|
|
$this->addAttachment($info['path'], $name);
|
2018-11-23 15:17:52 +01:00
|
|
|
|
|
|
|
$this->logs['prints'][] = $print['id'];
|
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
|
|
|
}
|
|
|
|
}
|