Skip to content

Instantly share code, notes, and snippets.

@pamanes
Created July 10, 2023 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pamanes/4bd1664f1f394efbc111bcf44a838681 to your computer and use it in GitHub Desktop.
Save pamanes/4bd1664f1f394efbc111bcf44a838681 to your computer and use it in GitHub Desktop.
Azure PowerShell Task for Build Pipeline E-Mail Send
# Required variables
$env:SYSTEM_ACCESSTOKEN
$env:SYSTEM_TEAMFOUNDATIONSERVERURI
$env:SYSTEM_TEAMPROJECTID
$env:SYSTEM_TEAMPROJECT
$workItemId = YOUR_WORK_ITEM_ID_HERE
$output = "BODY OF EMAIL HERE"
# Derived variables
$adoHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$env:SYSTEM_ACCESSTOKEN")) }
$organization = ($env:SYSTEM_TEAMFOUNDATIONSERVERURI -split "/")[3]
$emailAddresses = @()
$tfIds = @()
$emailAddresses+="YOUR_EMAIL_HERE@DOMAIN.com"
$tfIds+="YOUR_TFID_HERE"
# Prepare email body
$requestBody = @{
message = @{
to = @{
emailAddresses = $emailAddresses
tfIds = $tfIds
}
cc = @{}
replyTo = @{}
body = $output
subject = "SUBJECT HERE"
}
projectId = $env:SYSTEM_TEAMPROJECTID
ids = @($workItemId)
}
$arguments = @{
Uri = ("{0}{1}/_apis/wit/sendmail?api-version=7.0" -f $env:SYSTEM_TEAMFOUNDATIONSERVERURI, $env:SYSTEM_TEAMPROJECT)
Method = 'POST'
Headers = $adoHeader
Body = ConvertTo-Json -InputObject $requestBody -Depth 5
ContentType = 'application/json; charset=utf-8'
}
Invoke-RestMethod @arguments
@ggirodda
Copy link

ggirodda commented Aug 1, 2023

Hi @pamanes , this is the only helpful answer I found in the net. Did you try to send body content in html format? In my case the html is displayied as text when I receive the email.
Thank you for any information

@pamanes
Copy link
Author

pamanes commented Aug 4, 2023

hi @ggirodda , I haven't been able to send HTML, even though the docs state that the body is in HTML format!
https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/send-mail/send-mail?view=azure-devops-rest-7.0

thanks, and I'm glad this worked out for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment