Az Function App PowerShellPSHTML

Working with Azure Function App using PowerShell and PSHTML module – Part 3

In this blog post I will show a simple method to send email to the given address (Input form with a email input field and a submit button). Yes, you can think alternate for Office365 SMTP like send grid and mail jet services. But the ASKS is to send email with HTML body to given mail address. So, I opted for existing O365 to save time!


Simple steps are as follows

  1. Add your O365 user name and password in key vaults.
  2. Add the key vaults secrets in your functions app settings.
  3. Just follow the documentation for Azure Functions—Key Vault integration
  4. Create two functions with HTTP triggers


using namespace System.Net
param($Request, $TriggerMetadata)
$html = html -Content {
    head -Content {
        link -rel "stylesheet" -href "" -integrity "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" -crossorigin "anonymous"
        link -rel "stylesheet" -href ""
        link -rel "stylesheet" -href ""
        link -rel "stylesheet" -href ""
        link -rel "stylesheet" -href ""
        link -rel "stylesheet" -href ""
        script -src ""
        script -src ""
        script -src "" -integrity "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" -crossorigin "anonymous"
        script -src "" -integrity "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" -crossorigin "anonymous"
        script -src "" -integrity "sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" -crossorigin "anonymous"
        title -Content "iContact"
    body -Content {
        div -Class 'jumbotron' -Content {
            h1 -Class 'display-4' -Content "O365 Email..."
        form -action '/api/iSendEmail' -method 'post' -enctype 'application/x-www-form-urlencoded' -target _blank -Content {
            div -Class 'form-group' -Content {
                input -type email -name email -Class 'form-control'
            button -Attributes @{type = 'submit' } -Class 'btn btn-primary' -Content 'Send Email'
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'text/html' }
        StatusCode = [HttpStatusCode]::OK
        Body       = $html


using namespace System.Net;
using namespace System.Web;

param($Request, $TriggerMetadata)

$formdata = ([ordered]@{ })
$DecodedBody = [System.Web.HttpUtility]::UrlDecode($Request.Body)
($($DecodedBody) -split "&").ForEach( { $value = $_.split("="); $formdata.Add($value[0], $value[1]) })

$O365UserName = [System.Environment]::GetEnvironmentVariable('O365AdminUserName')
$O365Password = [System.Environment]::GetEnvironmentVariable('O365AdminPassword') | ConvertTo-SecureString -AsPlainText -Force
$Credential = [pscredential]::new($O365UserName, $O365Password)

$Body | ConvertTo-Html
$MailParams = @{
    From       = ''
    To         = $($
    Subject    = 'Test mail from Az Function App...'
    Body       = @"
  Hi Chen V,
    This is a test mail from Azure Function app using the Office 365 SMTP server. 

  Chen V
    SMTPServer = ''
    Usessl     = $true
    Credential = $Credential

Send-MailMessage @MailParams 

Push-OutputBinding -name Response -Value ([HttpResponseContext]@{
        headers    = @{'content-type' = 'application/json' }
        StatusCode = [HttpStatusCode]::OK
        Body       = [pscustomobject]@{
            formdata = $formdata
            UserName = $O365UserName
            Message  = "Message Sent from O365"
        } | ConvertTo-Json

Enjoy PowerShell – In my next blog we will discuss and demo the event driven automation using functions app with PowerShell.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *