Skip to content

Commit 4578a8d

Browse files
committed
fix: gestione errori in fase di invio email
1 parent be3d6bf commit 4578a8d

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

modules/emails/src/EmailTask.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,20 @@ public function execute()
5656
try {
5757
$email = EmailNotification::build($mail);
5858
$email->send();
59-
} catch (Exception $e) {
59+
} catch (\Exception $e) {
6060
echo $e;
6161

6262
$result['response'] = 2;
6363
$result['message'] = tr('Errore durante l\'invio delle email: _ERR_', [
6464
'_ERR_' => $e->getMessage(),
6565
]).'<br>';
66+
67+
// Aggiorna l'email come fallita
68+
if (!empty($mail)) {
69+
$mail->failed_at = date('Y-m-d H:i:s');
70+
$mail->attempt = $mail->attempt + 1;
71+
$mail->save();
72+
}
6673
}
6774
}
6875

src/Notifications/EmailNotification.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ public function setMail($mail)
149149

150150
// Reply To
151151
if (!empty($mail->options['reply_to'])) {
152-
$this->AddReplyTo($mail->options['reply_to']);
152+
try {
153+
$this->AddReplyTo($mail->options['reply_to']);
154+
} catch (\Exception $e) {
155+
// Ignora gli errori per indirizzi email non validi e continua
156+
$logger = logger_osm();
157+
$logger->addRecord(\Monolog\Logger::WARNING, 'Errore durante l\'impostazione del Reply-To: '.$e->getMessage(), [
158+
'reply_to' => $mail->options['reply_to'],
159+
]);
160+
}
153161
}
154162

155163
// Oggetto
@@ -244,7 +252,16 @@ public function addUpload($file_id)
244252
{
245253
$attachment = database()->fetchOne('SELECT * FROM zz_files WHERE id = '.prepare($file_id));
246254

247-
$this->addAttachment(base_dir().'/'.\Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename'], $attachment['original']);
255+
try {
256+
$this->addAttachment(base_dir().'/'.\Uploads::getDirectory($attachment['id_module'], $attachment['id_plugin']).'/'.$attachment['filename'], $attachment['original']);
257+
} catch (\Exception $e) {
258+
// Ignora gli errori per allegati non validi e continua
259+
$logger = logger_osm();
260+
$logger->addRecord(\Monolog\Logger::WARNING, 'Errore durante l\'aggiunta dell\'allegato: '.$e->getMessage(), [
261+
'file_id' => $file_id,
262+
'filename' => $attachment['original'] ?? 'unknown',
263+
]);
264+
}
248265
}
249266

250267
/**
@@ -267,12 +284,22 @@ public function addReceiver($receiver, $type = null)
267284
}
268285

269286
if (!empty($email)) {
270-
if ($type == 'cc') {
271-
$this->AddCC($email, $name);
272-
} elseif ($type == 'bcc') {
273-
$this->AddBCC($email, $name);
274-
} else {
275-
$this->AddAddress($email, $name);
287+
try {
288+
if ($type == 'cc') {
289+
$this->AddCC($email, $name);
290+
} elseif ($type == 'bcc') {
291+
$this->AddBCC($email, $name);
292+
} else {
293+
$this->AddAddress($email, $name);
294+
}
295+
} catch (\Exception $e) {
296+
// Ignora gli errori per indirizzi email non validi e continua con gli altri
297+
// L'eccezione viene registrata ma non interrompe l'elaborazione
298+
$logger = logger_osm();
299+
$logger->addRecord(\Monolog\Logger::WARNING, 'Errore durante l\'aggiunta del destinatario: '.$e->getMessage(), [
300+
'email' => $email,
301+
'type' => $type,
302+
]);
276303
}
277304
}
278305
}

0 commit comments

Comments
 (0)