Skip to content

Commit

Permalink
Updated email notification to support multi-recipients (#22577)
Browse files Browse the repository at this point in the history
  • Loading branch information
vidai-msft authored Aug 16, 2023
1 parent e8e1e17 commit e8b236f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
6 changes: 3 additions & 3 deletions tools/TestFx/Live/SendLiveTestReport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ $summarySection = @"
"@

if ($errorsArr.Count -gt 0) {
$emailBody = $errorsArr | Sort-Object OSVersion, PSVersion, Module, Name | ConvertTo-Html -Property OSVersion, PSVersion, Module, Name, Exception, RetryException -Head $css -Title "Azure PowerShell Live Test Report" -PreContent "$summarySection<h1>Live Test Error Details</h1>"
$emailContent = $errorsArr | Sort-Object OSVersion, PSVersion, Module, Name | ConvertTo-Html -Property OSVersion, PSVersion, Module, Name, Exception, RetryException -Head $css -Title "Azure PowerShell Live Test Report" -PreContent "$summarySection<h1>Live Test Error Details</h1>"
}
else {
$emailBody = "<html><head>$css</head><body>$summarySection<div>No live test errors reported. Please check the overall status from Azure pipeline.</div></body></html>"
$emailContent = "<html><head>$css</head><body>$summarySection<div>No live test errors reported. Please check the overall status from Azure pipeline.</div></body></html>"
}

Send-EmailServiceMail -To "${env:EMAILTO}" -Subject $emailSubject -Body $emailBody
Send-EmailServiceMail -To "${env:EMAILTO}" -Subject $emailSubject -Content $emailContent -IsHtml
33 changes: 29 additions & 4 deletions tools/TestFx/Utilities/EmailServiceUtility.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,39 @@ function Send-EmailServiceMail {
[string] $Subject,

[Parameter(Mandatory)]
$Body
[string] $Content,

[Parameter()]
[switch] $IsHtml
)

$emailClient = [Azure.Communication.Email.EmailClient]::new($EmailServiceConnectionString)

Write-Host "Sending email..."
$emailClient.SendAsync([Azure.WaitUntil]::Completed, $EmailFrom, $To, $Subject, $Body).GetAwaiter().GetResult()
Write-Host "Finished sending email."
$emailContent = [Azure.Communication.Email.EmailContent]::new($Subject)
if ($IsHtml.IsPresent) {
$emailContent.Html = $Content
}
else {
$emailContent.PlainText = $Content
}

$emailTo = $To.Split(";", [StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
[Azure.Communication.Email.EmailAddress]::new($_)
}
$emailRecipients = [Azure.Communication.Email.EmailRecipients]::new([System.Collections.Generic.List[Azure.Communication.Email.EmailAddress]]$emailTo)

$emailMessage = [Azure.Communication.Email.EmailMessage]::new($EmailFrom, $emailRecipients, $emailContent)

Write-Host "##[section]Start sending email notification."

try {
$emailClient.Send([Azure.WaitUntil]::Completed, $emailMessage)
}
catch {
Write-Error "Failed to send email notification with error message: $($_.Exception.Message)."
}

Write-Host "##[section]Finished sending email notification."
}

InitializeEmailServicePackages

0 comments on commit e8b236f

Please sign in to comment.