Public/Shared/New-SpamfilterSetup.ps1

function New-SpamfilterSetup {
    param(
        [Parameter(Mandatory)]
        [string]$TenantId
        )
    Connect-CustomerGraph -CustomerTenantId $TenantId

    try {
        $DomainName = (Get-MgDomain | Where-Object { $_.IsDefault -eq $true }).Id
    } catch {
        throw "Failed to get default domain for tenant $($TenantId): $_"
    }

    $MXRecord = Get-MXRecord -TenantId $TenantId -DomainName $DomainName

    # We add it using a temporary wrong destination, to be able to enable reports. This is because Spamfilter.io does not support enabling reports for domains which it thinks has "catch-alls".
    New-SpamfilterDomain -DomainName $DomainName -Destination "mx1.onlinemail.io" | Out-Null
    Write-Host "Created new Spamfilter domain: $($DomainName)" -ForegroundColor Green
    Set-SpamfilterReports -DomainName $DomainName -Enabled $true | Out-Null
    Write-Host "Enabled reports for domain: $($DomainName)" -ForegroundColor Green
    # Now we set the correct destination from the 365 MX record.
    Set-SpamfilterDomainDestination -DomainName $DomainName -Destination $MXRecord | Out-Null
    Write-Host "Set correct destination for domain: $($DomainName)" -ForegroundColor Green

    Write-Host "Completed Spamfilter.io setup for tenant!" -ForegroundColor Green

    Write-Host ""
    Write-Host "### DNS overview for domain: $($DomainName) ###" -foregroundcolor Cyan
    Confirm-M365Records -DomainName $DomainName
}