# Test SMTP Authentication for notifications@dataforth.com
# This script tests SMTP authentication to verify credentials work

param(
    [string]$Password = $(Read-Host -Prompt "Enter password for notifications@dataforth.com" -AsSecureString | ConvertFrom-SecureString)
)

$SMTPServer = "smtp.office365.com"
$SMTPPort = 587
$Username = "notifications@dataforth.com"

Write-Host "[OK] Testing SMTP authentication..." -ForegroundColor Green
Write-Host "    Server: $SMTPServer"
Write-Host "    Port: $SMTPPort"
Write-Host "    Username: $Username"
Write-Host ""

try {
    # Create secure password
    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential($Username, $SecurePassword)

    # Create SMTP client
    $SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = $Credential

    # Create test message
    $MailMessage = New-Object System.Net.Mail.MailMessage
    $MailMessage.From = $Username
    $MailMessage.To.Add($Username)
    $MailMessage.Subject = "SMTP Test - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
    $MailMessage.Body = "This is a test message to verify SMTP authentication."

    Write-Host "[OK] Sending test email..." -ForegroundColor Green
    $SMTPClient.Send($MailMessage)

    Write-Host "[SUCCESS] SMTP authentication successful!" -ForegroundColor Green
    Write-Host "          Test email sent successfully." -ForegroundColor Green
    Write-Host ""
    Write-Host "[OK] The credentials work correctly." -ForegroundColor Green
    Write-Host "     If the website is still failing, check:" -ForegroundColor Yellow
    Write-Host "     - Website SMTP configuration" -ForegroundColor Yellow
    Write-Host "     - Firewall rules blocking port 587" -ForegroundColor Yellow
    Write-Host "     - IP address restrictions in M365" -ForegroundColor Yellow

} catch {
    Write-Host "[ERROR] SMTP authentication failed!" -ForegroundColor Red
    Write-Host "        Error: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host ""

    if ($_.Exception.Message -like "*authentication*") {
        Write-Host "[ISSUE] Authentication credentials are incorrect" -ForegroundColor Yellow
        Write-Host "        - Verify the password is correct" -ForegroundColor Yellow
        Write-Host "        - Check if MFA requires an app password" -ForegroundColor Yellow
    } elseif ($_.Exception.Message -like "*5.7.57*") {
        Write-Host "[ISSUE] SMTP AUTH is disabled for this tenant or user" -ForegroundColor Yellow
        Write-Host "        Run: Set-CASMailbox -Identity notifications@dataforth.com -SmtpClientAuthenticationDisabled `$false" -ForegroundColor Yellow
    } elseif ($_.Exception.Message -like "*connection*") {
        Write-Host "[ISSUE] Connection problem" -ForegroundColor Yellow
        Write-Host "        - Check firewall rules" -ForegroundColor Yellow
        Write-Host "        - Verify port 587 is accessible" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "================================================================"
Write-Host "Next: Check Exchange Online logs for more details"
Write-Host "================================================================"
