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!
Output
Simple steps are as follows
- Add your O365 user name and password in key vaults.
- Add the key vaults secrets in your functions app settings.
- Just follow the documentation for Azure Functions—Key Vault integration
- Create two functions with HTTP triggers
iAbout
using namespace System.Net param($Request, $TriggerMetadata) $html = html -Content { head -Content { link -rel "stylesheet" -href "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" -integrity "sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" -crossorigin "anonymous" link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-all.min.css" link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro.min.css" link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-colors.min.css" link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-rtl.min.css" link -rel "stylesheet" -href "https://cdn.metroui.org.ua/v4/css/metro-icons.min.css" script -src "https://code.jquery.com/jquery-3.3.1.min.js" script -src "https://cdn.metroui.org.ua/v4/js/metro.min.js" script -src "https://code.jquery.com/jquery-3.2.1.slim.min.js" -integrity "sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" -crossorigin "anonymous" script -src "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" -integrity "sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" -crossorigin "anonymous" script -src "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" -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 })
iSendEmail
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 = 'chendrayan@chensoffice365.onmicrosoft.com' To = $($formdata.email) 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. Regards, Chen V "@ SMTPServer = 'smtp.office365.com' 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.