Miglioramenti notifiche email

This commit is contained in:
Thomas Zilio 2018-09-20 14:41:01 +02:00
parent 3e3c97cac5
commit 1b9099d718
4 changed files with 130 additions and 127 deletions

View File

@ -70,39 +70,59 @@ if (filter('op') == 'link_file' || filter('op') == 'unlink_file') {
download($upload_dir.'/'.$rs[0]['filename'], $rs[0]['original']); download($upload_dir.'/'.$rs[0]['filename'], $rs[0]['original']);
} elseif (post('op') == 'send-email') { } elseif (post('op') == 'send-email') {
$template = Mail::getTemplate(post('template')); $id_template = post('template');
$id_account = $template['id_smtp'];
// Informazioni di log // Informazioni di log
Filter::set('get', 'id_email', $template['id']); Filter::set('get', 'id_email', $id_template);
$mail = new Mail($id_account); // Inizializzazione
$mail = new Notifications\EmailNotification();
// Conferma di lettura $mail->setTemplate($id_template, $id_record);
if (!empty(post('read_notify'))) {
$mail->ConfirmReadingTo = $mail->From;
}
// Template
$mail->setTemplate($template);
// Destinatari // Destinatari
$mail->addReceivers(post('destinatari'), post('tipo_destinatari')); $receivers = post('destinatari');
$types = post('tipo_destinatari');
foreach ($receivers as $key => $receiver) {
$mail->addReceiver($receiver, $types[$key]);
}
// Oggetto // Contenuti
$mail->Subject = post('subject'); $mail->setSubject(post('subject'));
$mail->setContent(post('body'));
// Allegati // Stampe da allegare
$mail->attach(post('prints'), post('attachments')); $prints = post('prints');
foreach ($prints as $print) {
$mail->addPrint($print, $id_record);
}
// Contenuto // Allegati originali
$mail->Body = post('body'); $files = post('attachments');
if (!empty($files)) {
// Allegati del record
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
foreach ($attachments as $attachment) {
$mail->addAttachment($upload_dir.'/'.$attachment['filename']);
}
// Allegati dell'Azienda predefinita
$anagrafiche = Modules::get('Anagrafiche');
$attachments = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module != '.prepare($id_module));
$directory = DOCROOT.'/'.Uploads::getDirectory($anagrafiche['id']);
foreach ($attachments as $attachment) {
$mail->addAttachment($directory.'/'.$attachment['filename']);
}
}
// Invio mail // Invio mail
if (!$mail->send()) { try {
flash()->error(tr("Errore durante l'invio dell'email").': '.$mail->ErrorInfo); $mail->send(true); // Il valore true impone la gestione degli errori tramite eccezioni
} else {
flash()->info(tr('Email inviata correttamente!')); flash()->info(tr('Email inviata correttamente!'));
} catch (PHPMailer\PHPMailer\Exception $e) {
flash()->error(tr("Errore durante l'invio dell'email").': '.$e->errorMessage());
} }
} }

View File

@ -122,9 +122,9 @@ switch (filter('op')) {
} }
// Controllo che non esista già un intervento collegato a questo promemoria e, se ho spuntato di creare l'intervento, creo già anche quello // Controllo che non esista già un intervento collegato a questo promemoria e, se ho spuntato di creare l'intervento, creo già anche quello
if ((empty($dbo->fetchArray("SELECT idintervento FROM co_promemoria WHERE id = '".((empty($idriga)) ? $id_record : $idriga)."'")[0]['idintervento'])) and ($post['pianifica_intervento'])) { if ((empty($dbo->fetchArray("SELECT idintervento FROM co_promemoria WHERE id = '".((empty($idriga)) ? $id_record : $idriga)."'")[0]['idintervento'])) && (post('pianifica_intervento'))) {
// pianificare anche l' intervento? // pianificare anche l' intervento?
// if ($post['pianifica_intervento']) { // if (post('pianifica_intervento')) {
/*$orario_inizio = post('orario_inizio'); /*$orario_inizio = post('orario_inizio');
$orario_fine = post('orario_fine');*/ $orario_fine = post('orario_fine');*/
@ -203,7 +203,7 @@ switch (filter('op')) {
} }
flash()->info(tr('Interventi pianificati correttamente')); flash()->info(tr('Interventi pianificati correttamente'));
} elseif ($post['pianifica_intervento']) { } elseif (post('pianifica_intervento')) {
flash()->warning(tr('Esiste già un intervento pianificato per il _DATE_', [ flash()->warning(tr('Esiste già un intervento pianificato per il _DATE_', [
'_DATE_' => Translator::dateToLocale($data_richiesta), '_DATE_' => Translator::dateToLocale($data_richiesta),
])); ]));

View File

@ -249,98 +249,31 @@ class Mail extends PHPMailer\PHPMailer\PHPMailer
} }
/** /**
* Aggiunge gli allegati all'email. * Aggiunge un destinatario.
* *
* @param array $prints * @param array $receiver
* @param array $files * @param array $type
*/ */
public function attach($prints, $files) public function addReceiver($receiver, $type = null)
{ {
$id_module = Modules::getCurrent()['id']; $pieces = explode('<', $receiver);
$id_record = App::getCurrentElement(); $count = count($pieces);
// Elenco degli allegati $name = null;
$attachments = []; if ($count > 1) {
$email = substr(end($pieces), 0, -1);
// Stampe $name = substr($receiver, 0, strpos($receiver, '<'.$email));
foreach ($prints as $print) { } else {
$print = Prints::get($print); $email = $receiver;
// Utilizzo di una cartella particolare per il salvataggio temporaneo degli allegati
$filename = DOCROOT.'/files/attachments/'.$print['title'].' - '.$id_record.'.pdf';
Prints::render($print['id'], $id_record, $filename);
$attachments[] = [
'path' => $filename,
'name' => $print['title'].'.pdf',
];
} }
// Allegati del record if (!empty($email)) {
$selected = []; if ($type == 'cc') {
if (!empty($files)) { $this->AddCC($email, $name);
$selected = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record)); } elseif ($type == 'bcc') {
} $this->AddBCC($email, $name);
foreach ($selected as $attachment) {
$attachments[] = [
'path' => $upload_dir.'/'.$attachment['filename'],
'name' => $attachment['name'],
];
}
// Allegati dell'Azienda predefinita
$anagrafiche = Modules::get('Anagrafiche');
$selected = [];
if (!empty($files)) {
$selected = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module != '.prepare($id_module));
}
foreach ($selected as $attachment) {
$attachments[] = [
'path' => DOCROOT.'/files/'.$anagrafiche['directory'].'/'.$attachment['filename'],
'name' => $attachment['name'],
];
}
// Aggiunta allegati
foreach ($attachments as $attachment) {
$this->AddAttachment($attachment['path'], $attachment['name']);
}
}
/**
* Aggiunge i destinatari.
*
* @param array $receivers
* @param array $types
*/
public function addReceivers($receivers, $types = [])
{
foreach ($receivers as $key => $destinatario) {
$type = $types[$key] ?: 'a';
$pieces = explode('<', $destinatario);
$count = count($pieces);
$name = null;
if ($count > 1) {
$email = substr(end($pieces), 0, -1);
$name = substr($destinatario, 0, strpos($destinatario, '<'.$email));
} else { } else {
$email = $destinatario; $this->AddAddress($email, $name);
}
if (!empty($email)) {
if ($type == 'a') {
$this->AddAddress($email, $name);
} elseif ($type == 'cc') {
$this->AddCC($email, $name);
} elseif ($type == 'bcc') {
$this->AddBCC($email, $name);
}
} }
} }
} }

View File

@ -7,10 +7,12 @@ use Prints;
class EmailNotification extends Notification class EmailNotification extends Notification
{ {
protected $subject = null;
protected $readNotify = false;
protected $template = null; protected $template = null;
protected $account = null; protected $account = null;
protected $attachments = null; protected $attachments = null;
protected $subject = null;
/** /**
* Restituisce l'account email della notifica. * Restituisce l'account email della notifica.
@ -46,25 +48,31 @@ class EmailNotification extends Notification
* Imposta il template della notifica. * Imposta il template della notifica.
* *
* @param string|int $value * @param string|int $value
* @param int $id_record
*/ */
public function setTemplate($value, $id_record) public function setTemplate($value, $id_record = null)
{ {
$this->template = $value; $this->template = $value;
$template = $this->getTemplate(); $template = $this->getTemplate();
$variables = Mail::getTemplateVariables($template['id'], $id_record);
// Sostituzione delle variabili di base $this->setReadNotify($template['read_notify']);
$replaces = [];
foreach ($variables as $key => $value) {
$replaces['{'.$key.'}'] = $value;
}
$body = replace($template['body'], $replaces);
$subject = replace($template['subject'], $replaces);
$this->setContent($body);
$this->setSubject($subject);
$this->setAccount($template['id_smtp']); $this->setAccount($template['id_smtp']);
if (!empty($id_record)) {
$variables = Mail::getTemplateVariables($template['id'], $id_record);
// Sostituzione delle variabili di base
$replaces = [];
foreach ($variables as $key => $value) {
$replaces['{'.$key.'}'] = $value;
}
$body = replace($template['body'], $replaces);
$subject = replace($template['subject'], $replaces);
$this->setContent($body);
$this->setSubject($subject);
}
} }
/** /**
@ -164,10 +172,44 @@ class EmailNotification extends Notification
$this->subject = $value; $this->subject = $value;
} }
public function send() /**
* Restituisce il titolo della notifica.
*
* @return bool
*/
public function getReadNotify()
{
return $this->readNotify;
}
/**
* Imposta il titolo della notifica.
*
* @param bool $value
*/
public function setReadNotify($value)
{
$this->readNotify = boolval($value);
}
/**
* Aggiunge un destinataro alla notifica.
*
* @param string $value
* @param string $type
*/
public function addReceiver($value, $type = null)
{
$this->receivers[] = [
'email' => $value,
'type' => $type,
];
}
public function send($exceptions = false)
{ {
$account = $this->getAccount(); $account = $this->getAccount();
$mail = new Mail($account['id']); $mail = new Mail($account['id'], $exceptions);
// Template // Template
$template = $this->getTemplate(); $template = $this->getTemplate();
@ -176,12 +218,20 @@ class EmailNotification extends Notification
} }
// Destinatari // Destinatari
$mail->addReceivers($this->getReceivers()); $receivers = $this->getReceivers();
foreach ($receivers as $receiver) {
$mail->addReceiver($receiver['email'], $receiver['type']);
}
// Allegati // Allegati
$attachments = $this->getAttachments(); $attachments = $this->getAttachments();
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
$this->AddAttachment($attachment['path'], $attachment['name']); $mail->AddAttachment($attachment['path'], $attachment['name']);
}
// Conferma di lettura
if (!empty($this->getReadNotify())) {
$mail->ConfirmReadingTo = $mail->From;
} }
// Oggetto // Oggetto