Private/Send-AuditReport.ps1
|
function Send-AuditReport { <# .SYNOPSIS Sends an HTML audit report via email. .DESCRIPTION Internal helper that delivers the certificate audit HTML report as an email attachment using Send-MailMessage. The HTML body is also included inline for mail clients that support it. .PARAMETER ReportPath Full path to the HTML report file. .PARAMETER SmtpServer SMTP relay server hostname or IP. .PARAMETER To One or more recipient email addresses. .PARAMETER From Sender email address. .PARAMETER Subject Email subject line. #> [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateScript({ Test-Path $_ -PathType Leaf })] [string]$ReportPath, [Parameter(Mandatory)] [string]$SmtpServer, [Parameter(Mandatory)] [string[]]$To, [Parameter(Mandatory)] [string]$From, [Parameter()] [string]$Subject ) if (-not $Subject) { $Subject = 'Certificate Audit Report - ' + (Get-Date -Format 'yyyy-MM-dd') } Write-Verbose "Sending report from $From to $($To -join ', ') via $SmtpServer" $htmlBody = Get-Content -Path $ReportPath -Raw -Encoding UTF8 $mailParams = @{ From = $From To = $To Subject = $Subject Body = $htmlBody BodyAsHtml = $true SmtpServer = $SmtpServer Attachments = $ReportPath Priority = 'High' ErrorAction = 'Stop' } try { Send-MailMessage @mailParams Write-Verbose "Report emailed successfully to $($To -join ', ')." } catch { Write-Error "Failed to send audit report email: $_" throw } } |