Modules/Send-SendGridMail.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
function Send-SendGridMail { Param ( [String] $ApiKey = $env:SENDGRID_API_KEY, [ValidateSet( "text/plain", "text/html")] [String] $ContentType = "text/plain", [Parameter(Mandatory)] [String] $To, [String] $From = $env:SENDGRID_FROM, [Parameter(Mandatory)] [String] $Subject, [Parameter(Mandatory)] [String] $Message ) # How to Create an API Key # https://sendgrid.com/docs/ui/account-and-settings/api-keys/#creating-an-api-key # If ApiKey is missing use the variable If(!$ApiKey) { throw "Missing 'ApiKey' parameter! Supply the ApiKey value or set the SENDGRID_API_KEY environment variable. " + "How to create an API Key https://sendgrid.com/docs/ui/account-and-settings/api-keys/#creating-an-api-key" } If(!$From) { throw "Missing 'From' parameter! Supply the From value or set the SENDGRID_FROM environment variable" } $data = @{ personalizations = @(@{"to" = @( @{"email" = "$To"})}) from = @{"email" = "$From"} subject = "$Subject" content = @(@{type = "$ContentType"; value = "$Message"}) } $webRequestParameters = @{ Uri = "https://api.sendgrid.com/v3/mail/send" ContentType = "application/json" Headers = @{Authorization = "Bearer $ApiKey"} Method = "Post" Body = ($data | ConvertTo-Json -Depth 5 -Compress) } Invoke-WebRequest @webRequestParameters } |