Skip to content

Instantly share code, notes, and snippets.

@TomasBouda
Last active April 28, 2019 10:17
Show Gist options
  • Save TomasBouda/e3a1c230a86f9d0f37dfbdf2bc056c95 to your computer and use it in GitHub Desktop.
Save TomasBouda/e3a1c230a86f9d0f37dfbdf2bc056c95 to your computer and use it in GitHub Desktop.
Send email trough Send Grid API with powershell
function Send-GridEmail(){
Param(
[Parameter(Mandatory=$true)]
[string]$From,
[Parameter(Mandatory=$true)]
[object[]]$To,
[Parameter(Mandatory=$true)]
[string]$Subject,
[Parameter(Mandatory=$true)]
[string]$Body,
[Parameter(Mandatory=$false)]
[object[]]$CopyTo
)
$SENDGRID_API_KEY = "****"
$HEADERS = @{"Authorization" = "Bearer $SENDGRID_API_KEY"; "Content-Type" = "application/json"}
$BODY_OBJ = @{
personalizations = @(
@{
to = $To
cc = $CopyTo
}
)
from = @{
email = $From
}
subject = $Subject
content = @(
@{
type = "text/plain"
value = $Body
}
)
}
$JsonBody = ConvertTo-Json -InputObject $BODY_OBJ -Depth 10
Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $HEADERS -Body $JsonBody
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment