2017-09-14 11:40:13 +02:00
|
|
|
<?php
|
|
|
|
|
2018-09-20 11:39:03 +02:00
|
|
|
use Models\MailAccount;
|
|
|
|
|
2017-09-14 11:40:13 +02:00
|
|
|
/**
|
2018-02-22 12:35:22 +01:00
|
|
|
* Classe per gestire le email in base alle impostazioni, basata sul framework open-source PHPMailer.
|
2017-09-14 11:40:13 +02:00
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
2018-01-02 17:01:47 +01:00
|
|
|
class Mail extends PHPMailer\PHPMailer\PHPMailer
|
2017-09-14 11:40:13 +02:00
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
/** @var array Elenco degli account email disponibili */
|
|
|
|
protected static $accounts = [];
|
|
|
|
|
2018-02-20 16:46:23 +01:00
|
|
|
/** @var array Elenco dei template email disponibili */
|
|
|
|
protected static $templates = [];
|
2018-06-23 15:41:32 +02:00
|
|
|
protected static $references = [];
|
2018-02-20 16:46:23 +01:00
|
|
|
/** @var array Elenco dei template email per modulo */
|
|
|
|
protected static $modules = [];
|
|
|
|
|
2017-09-14 11:40:13 +02:00
|
|
|
protected $infos = [];
|
|
|
|
|
2017-09-22 15:16:56 +02:00
|
|
|
/**
|
2018-07-10 12:07:48 +02:00
|
|
|
* Restituisce tutte le informazioni di tutti gli account email presenti.
|
2017-09-22 15:16:56 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAccounts()
|
|
|
|
{
|
2018-09-20 11:39:03 +02:00
|
|
|
return MailAccount::getAll();
|
2017-09-22 15:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 12:07:48 +02:00
|
|
|
* Restituisce le informazioni relative a un singolo account email specificato.
|
2017-09-22 15:16:56 +02:00
|
|
|
*
|
2018-09-20 11:39:03 +02:00
|
|
|
* @param string|int $account
|
2017-09-22 15:16:56 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get($account = null)
|
|
|
|
{
|
2018-09-20 11:39:03 +02:00
|
|
|
$accounts = self::getAccounts();
|
|
|
|
|
|
|
|
$result = MailAccount::get($account);
|
2017-09-22 15:16:56 +02:00
|
|
|
|
2018-10-25 12:34:18 +02:00
|
|
|
if (empty($result)) {
|
2018-09-27 12:54:57 +02:00
|
|
|
$result = $accounts->first(function ($item) {
|
|
|
|
return !empty($item->predefined);
|
2018-09-20 11:39:03 +02:00
|
|
|
});
|
2018-10-25 12:34:18 +02:00
|
|
|
}
|
2017-09-22 15:16:56 +02:00
|
|
|
|
2018-09-20 11:39:03 +02:00
|
|
|
return $result;
|
2017-09-22 15:16:56 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 16:46:23 +01:00
|
|
|
/**
|
2018-07-10 12:07:48 +02:00
|
|
|
* Restituisce tutte le informazioni di tutti i template presenti.
|
2018-02-20 16:46:23 +01:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getTemplates()
|
|
|
|
{
|
|
|
|
if (empty(self::$templates)) {
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2018-02-20 16:46:23 +01:00
|
|
|
|
2018-07-17 08:05:19 +02:00
|
|
|
$results = $database->fetchArray('SELECT * FROM zz_emails WHERE deleted_at IS NULL');
|
2018-02-20 16:46:23 +01:00
|
|
|
|
|
|
|
$templates = [];
|
2018-06-23 18:35:08 +02:00
|
|
|
$references = [];
|
2018-02-20 16:46:23 +01:00
|
|
|
|
2018-06-23 15:41:32 +02:00
|
|
|
// Inizializzazione dei riferimenti
|
|
|
|
foreach (Modules::getModules() as $module) {
|
|
|
|
self::$modules[$module['id']] = [];
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:46:23 +01:00
|
|
|
foreach ($results as $result) {
|
|
|
|
$templates[$result['id']] = $result;
|
2018-06-23 15:41:32 +02:00
|
|
|
$references[$result['name']] = $result['id'];
|
2018-02-20 16:46:23 +01:00
|
|
|
|
|
|
|
self::$modules[$result['id_module']][] = $result['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$templates = $templates;
|
2018-06-23 15:41:32 +02:00
|
|
|
self::$references = $references;
|
2018-02-20 16:46:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative a un singolo template specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $template
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getTemplate($template)
|
|
|
|
{
|
2018-06-23 18:35:08 +02:00
|
|
|
$templates = self::getTemplates();
|
|
|
|
|
2018-06-23 15:41:32 +02:00
|
|
|
if (!is_numeric($template) && !empty(self::$references[$template])) {
|
|
|
|
$template = self::$references[$template];
|
2018-02-20 16:46:23 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 18:35:08 +02:00
|
|
|
return $templates[$template];
|
2018-02-20 16:46:23 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 17:57:16 +01:00
|
|
|
/**
|
2018-07-10 12:07:48 +02:00
|
|
|
* Restituisce le variabili relative a un singolo template specificato.
|
2018-02-20 17:57:16 +01:00
|
|
|
*
|
|
|
|
* @param string|int $template
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-21 17:47:43 +01:00
|
|
|
public static function getTemplateVariables($template, $id_record)
|
2018-02-20 17:57:16 +01:00
|
|
|
{
|
|
|
|
$template = self::getTemplate($template);
|
2018-02-21 17:47:43 +01:00
|
|
|
|
2018-09-20 12:05:22 +02:00
|
|
|
$dbo = $database = database();
|
2018-02-20 17:57:16 +01:00
|
|
|
|
|
|
|
// Lettura delle variabili nei singoli moduli
|
2018-06-26 14:26:40 +02:00
|
|
|
$variables = include Modules::filepath($template['id_module'], 'variables.php');
|
2018-02-20 17:57:16 +01:00
|
|
|
|
|
|
|
return (array) $variables;
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:46:23 +01:00
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative ai template di un singolo modulo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getModuleTemplates($module)
|
|
|
|
{
|
|
|
|
$module_id = Modules::get($module)['id'];
|
|
|
|
|
2018-02-20 17:57:16 +01:00
|
|
|
self::getTemplates();
|
|
|
|
|
2018-02-20 16:46:23 +01:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ((array) self::$modules[$module_id] as $value) {
|
|
|
|
$result[] = self::getTemplate($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-09-22 15:16:56 +02:00
|
|
|
public function __construct($account = null, $exceptions = null)
|
2017-09-14 11:40:13 +02:00
|
|
|
{
|
|
|
|
parent::__construct($exceptions);
|
|
|
|
|
2018-07-10 16:44:09 +02:00
|
|
|
$this->CharSet = 'UTF-8';
|
|
|
|
|
2017-09-14 11:40:13 +02:00
|
|
|
// Configurazione di base
|
2017-09-22 15:16:56 +02:00
|
|
|
$config = self::get($account);
|
2017-09-14 11:40:13 +02:00
|
|
|
|
|
|
|
// Preparazione email
|
|
|
|
$this->IsHTML(true);
|
|
|
|
|
2018-02-27 16:19:13 +01:00
|
|
|
if (!empty($config['server'])) {
|
2017-09-14 11:40:13 +02:00
|
|
|
$this->IsSMTP(true);
|
|
|
|
|
|
|
|
// Impostazioni di debug
|
2018-10-04 17:41:31 +02:00
|
|
|
$this->SMTPDebug = App::debug() ? 2 : 0;
|
2017-09-14 11:40:13 +02:00
|
|
|
$this->Debugoutput = function ($str, $level) {
|
2018-09-28 15:17:26 +02:00
|
|
|
$this->infos[] = $str;
|
2017-09-14 11:40:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Impostazioni dell'host
|
2018-02-27 16:19:13 +01:00
|
|
|
$this->Host = $config['server'];
|
2017-09-14 11:40:13 +02:00
|
|
|
$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
|
2017-09-22 15:16:56 +02:00
|
|
|
if (in_array(strtolower($config['encryption']), ['ssl', 'tls'])) {
|
|
|
|
$this->SMTPSecure = strtolower($config['encryption']);
|
2017-09-14 11:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:57:16 +01:00
|
|
|
$this->From = $config['from_address'];
|
2018-07-08 15:42:20 +02:00
|
|
|
$this->FromName = $config['from_name'];
|
2018-02-20 17:57:16 +01:00
|
|
|
|
2017-09-14 11:40:13 +02:00
|
|
|
$this->WordWrap = 78;
|
|
|
|
}
|
|
|
|
|
2018-09-20 11:39:03 +02:00
|
|
|
/**
|
|
|
|
* Testa la connessione al server SMTP.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function testSMTP()
|
|
|
|
{
|
|
|
|
if ($this->smtpConnect()) {
|
|
|
|
$this->smtpClose();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invia l'email impostata.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-14 11:40:13 +02:00
|
|
|
public function send()
|
|
|
|
{
|
|
|
|
if (empty($this->AltBody)) {
|
|
|
|
$this->AltBody = strip_tags($this->Body);
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:16:43 +02:00
|
|
|
$exception = null;
|
|
|
|
try {
|
|
|
|
$result = parent::send();
|
|
|
|
} catch (PHPMailer\PHPMailer\Exception $e) {
|
|
|
|
$result = false;
|
|
|
|
$exception = $e;
|
|
|
|
}
|
2017-09-14 11:40:13 +02:00
|
|
|
|
|
|
|
$this->SmtpClose();
|
|
|
|
|
|
|
|
// Segnalazione degli errori
|
2018-09-28 15:16:43 +02:00
|
|
|
if (!$result) {
|
|
|
|
$logger = logger();
|
|
|
|
foreach ($this->infos as $info) {
|
|
|
|
$logger->addRecord(\Monolog\Logger::ERROR, $info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($exception)) {
|
|
|
|
throw $exception;
|
2017-09-14 11:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2018-06-29 16:19:48 +02:00
|
|
|
|
2018-09-20 11:39:03 +02:00
|
|
|
public function setTemplate(array $template)
|
|
|
|
{
|
|
|
|
// Reply To
|
|
|
|
if (!empty($template['reply_to'])) {
|
|
|
|
$this->AddReplyTo($template['reply_to']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CC
|
|
|
|
if (!empty($template['cc'])) {
|
|
|
|
$this->AddCC($template['cc']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BCC
|
|
|
|
if (!empty($template['bcc'])) {
|
|
|
|
$this->AddBCC($template['bcc']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
2018-09-20 14:41:01 +02:00
|
|
|
* Aggiunge un destinatario.
|
2018-07-10 12:07:48 +02:00
|
|
|
*
|
2018-09-20 14:41:01 +02:00
|
|
|
* @param array $receiver
|
|
|
|
* @param array $type
|
2018-07-10 12:07:48 +02:00
|
|
|
*/
|
2018-09-20 14:41:01 +02:00
|
|
|
public function addReceiver($receiver, $type = null)
|
2018-07-10 12:07:48 +02:00
|
|
|
{
|
2018-09-20 14:41:01 +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-07-10 12:07:48 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 14:41:01 +02:00
|
|
|
if (!empty($email)) {
|
|
|
|
if ($type == 'cc') {
|
|
|
|
$this->AddCC($email, $name);
|
|
|
|
} elseif ($type == 'bcc') {
|
|
|
|
$this->AddBCC($email, $name);
|
2018-07-10 12:07:48 +02:00
|
|
|
} else {
|
2018-09-20 14:41:01 +02:00
|
|
|
$this->AddAddress($email, $name);
|
2018-07-10 12:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 11:40:13 +02:00
|
|
|
}
|