Private/Send-NSPConditionalAccessCoverageMail.ps1

function Send-NSPConditionalAccessCoverageMail {
    <#
    .SYNOPSIS
        Sends a scheduled coverage report through Graph, as the configured sender mailbox. Throws on failure.
    .DESCRIPTION
        Plain text: status, messages, warnings, and one line per add. The coverage app can send only
        as mailboxes Exchange RBAC for Applications scopes it to (Get-NSPConditionalAccessCoverageMailSetup).
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory)]$Report,
        [Parameter(Mandatory)][string]$From,
        [Parameter(Mandatory)][string[]]$To,
        [string]$TenantLabel
    )

    $lines = [System.Collections.Generic.List[string]]::new()
    $lines.Add("Conditional Access coverage for $TenantLabel")
    $lines.Add("Run finished $($Report.FinishedAtUtc) (UTC). Status: $($Report.Status).")
    $lines.Add('')
    foreach ($message in @($Report.Messages)) { if ($message) { $lines.Add($message) } }
    foreach ($warning in @($Report.Warnings)) { if ($warning) { $lines.Add("Warning: $warning") } }
    if (@($Report.Results).Count) {
        $lines.Add('')
        $lines.Add('Users handled (adding to MFACatchAll restricts their access):')
        foreach ($result in @($Report.Results)) { $lines.Add(" $($result.ItemId): $($result.Result). $($result.Message)") }
    }
    $lines.Add('')
    $lines.Add("Full report on the job host: $($Report.Path)")

    $body = @{
        message = @{
            subject = "Conditional Access coverage: $($Report.Status) ($TenantLabel)"
            body = @{ contentType = 'Text'; content = ($lines -join "`r`n") }
            toRecipients = @(@($To) | ForEach-Object { @{ emailAddress = @{ address = $_ } } })
        }
        saveToSentItems = $false
    }
    if ($PSCmdlet.ShouldProcess(($To -join ', '), "Send coverage report from $From")) {
        Invoke-NSPGraphRequest -Method POST -Uri "users/$([uri]::EscapeDataString($From))/sendMail" -Body $body | Out-Null
    }
}