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']);
} elseif (post('op') == 'send-email') {
$template = Mail::getTemplate(post('template'));
$id_account = $template['id_smtp'];
$id_template = post('template');
// Informazioni di log
Filter::set('get', 'id_email', $template['id']);
Filter::set('get', 'id_email', $id_template);
$mail = new Mail($id_account);
// Conferma di lettura
if (!empty(post('read_notify'))) {
$mail->ConfirmReadingTo = $mail->From;
}
// Template
$mail->setTemplate($template);
// Inizializzazione
$mail = new Notifications\EmailNotification();
$mail->setTemplate($id_template, $id_record);
// 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
$mail->Subject = post('subject');
// Contenuti
$mail->setSubject(post('subject'));
$mail->setContent(post('body'));
// Allegati
$mail->attach(post('prints'), post('attachments'));
// Stampe da allegare
$prints = post('prints');
foreach ($prints as $print) {
$mail->addPrint($print, $id_record);
}
// Contenuto
$mail->Body = post('body');
// Allegati originali
$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
if (!$mail->send()) {
flash()->error(tr("Errore durante l'invio dell'email").': '.$mail->ErrorInfo);
} else {
try {
$mail->send(true); // Il valore true impone la gestione degli errori tramite eccezioni
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
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?
// if ($post['pianifica_intervento']) {
// if (post('pianifica_intervento')) {
/*$orario_inizio = post('orario_inizio');
$orario_fine = post('orario_fine');*/
@ -203,7 +203,7 @@ switch (filter('op')) {
}
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_', [
'_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 $files
* @param array $receiver
* @param array $type
*/
public function attach($prints, $files)
public function addReceiver($receiver, $type = null)
{
$id_module = Modules::getCurrent()['id'];
$id_record = App::getCurrentElement();
$pieces = explode('<', $receiver);
$count = count($pieces);
// Elenco degli allegati
$attachments = [];
// Stampe
foreach ($prints as $print) {
$print = Prints::get($print);
// 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',
];
$name = null;
if ($count > 1) {
$email = substr(end($pieces), 0, -1);
$name = substr($receiver, 0, strpos($receiver, '<'.$email));
} else {
$email = $receiver;
}
// Allegati del record
$selected = [];
if (!empty($files)) {
$selected = $dbo->fetchArray('SELECT * FROM zz_files WHERE id IN ('.implode(',', $files).') AND id_module = '.prepare($id_module).' AND id_record = '.prepare($id_record));
}
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));
if (!empty($email)) {
if ($type == 'cc') {
$this->AddCC($email, $name);
} elseif ($type == 'bcc') {
$this->AddBCC($email, $name);
} else {
$email = $destinatario;
}
if (!empty($email)) {
if ($type == 'a') {
$this->AddAddress($email, $name);
} elseif ($type == 'cc') {
$this->AddCC($email, $name);
} elseif ($type == 'bcc') {
$this->AddBCC($email, $name);
}
$this->AddAddress($email, $name);
}
}
}

View File

@ -7,10 +7,12 @@ use Prints;
class EmailNotification extends Notification
{
protected $subject = null;
protected $readNotify = false;
protected $template = null;
protected $account = null;
protected $attachments = null;
protected $subject = null;
/**
* Restituisce l'account email della notifica.
@ -46,25 +48,31 @@ class EmailNotification extends Notification
* Imposta il template della notifica.
*
* @param string|int $value
* @param int $id_record
*/
public function setTemplate($value, $id_record)
public function setTemplate($value, $id_record = null)
{
$this->template = $value;
$template = $this->getTemplate();
$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);
$this->setReadNotify($template['read_notify']);
$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;
}
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();
$mail = new Mail($account['id']);
$mail = new Mail($account['id'], $exceptions);
// Template
$template = $this->getTemplate();
@ -176,12 +218,20 @@ class EmailNotification extends Notification
}
// Destinatari
$mail->addReceivers($this->getReceivers());
$receivers = $this->getReceivers();
foreach ($receivers as $receiver) {
$mail->addReceiver($receiver['email'], $receiver['type']);
}
// Allegati
$attachments = $this->getAttachments();
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