From 0e74d9ba6ce796f3c1c431a0ed449b9fdae9dd79 Mon Sep 17 00:00:00 2001 From: Mattia Tadini Date: Wed, 19 Nov 2025 12:20:15 +0000 Subject: [PATCH] Update AdHoc-Backup.ps1 --- AdHoc-Backup.ps1 | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) diff --git a/AdHoc-Backup.ps1 b/AdHoc-Backup.ps1 index 5efc0bb..2d0165d 100644 --- a/AdHoc-Backup.ps1 +++ b/AdHoc-Backup.ps1 @@ -25,6 +25,7 @@ try { $ScriptPath = $MyInvocation.MyCommand.Path $ScriptDir = Split-Path -Parent $ScriptPath +$MailScript = Join-Path $ScriptDir 'send-mail.ps1' # Layout dell'installer GUI (se presente): .\bin\{RClone,7Zip,conf} $InstallerBinRoot = Join-Path $ScriptDir 'bin' @@ -693,34 +694,6 @@ function Apply-GenericRetention { #endregion ========================================================================== -#region ============================= Email ========================================= - -function Send-ReportMail { - param([string]$Subject,[string]$Body) - if (-not $MailEnabled) { return } - try { - $smtp = New-Object System.Net.Mail.SmtpClient($MailSmtpHost, $MailSmtpPort) - $smtp.EnableSsl = $true - $smtp.DeliveryMethod = [System.Net.Mail.SmtpDeliveryMethod]::Network - $smtp.UseDefaultCredentials = $false - if ($MailUseAuth) { - $smtp.Credentials = New-Object System.Net.NetworkCredential($MailUser, $MailPassword) - } - $msg = New-Object System.Net.Mail.MailMessage - $msg.From = $MailFrom - foreach ($rcpt in @($MailTo)) { - if ($rcpt) { $msg.To.Add($rcpt) } - } - $msg.Subject = $Subject - $msg.Body = $Body - $smtp.Send($msg) - Write-Log INFO "[MAIL] Inviata correttamente." - } catch { - Write-Log ERROR "[MAIL] Errore invio: $_" - } -} - -#endregion ========================================================================== #region ============================= MAIN ========================================== @@ -802,7 +775,7 @@ try { Write-Log INFO "Backup $status." $summary.Insert(0, "ESITO: $status`r`n") | Out-Null - Send-ReportMail -Subject ($MailSubjectPref + "OK " + $HostName) -Body $summary.ToString() + if (Test-Path -LiteralPath $MailScript) { . $MailScript -Subject ($MailSubjectPref + "OK " + $HostName) -Body $summary.ToString() } else { Write-Log WARN "Script di invio mail non trovato: $MailScript" } } catch { $msg = $_.Exception.Message @@ -810,7 +783,7 @@ try { $summary.Insert(0, "ESITO: FALLITO`r`n") | Out-Null $summary.AppendLine("") | Out-Null $summary.AppendLine("ERRORE: $msg") | Out-Null - Send-ReportMail -Subject ($MailSubjectPref + "ERRORE " + $HostName) -Body $summary.ToString() + if (Test-Path -LiteralPath $MailScript) { . $MailScript -Subject ($MailSubjectPref + "ERRORE " + $HostName) -Body $summary.ToString() } else { Write-Log WARN "Script di invio mail non trovato: $MailScript" } throw } finally { $summary.AppendLine("") | Out-Null