Last active
April 28, 2019 10:17
-
-
Save TomasBouda/e3a1c230a86f9d0f37dfbdf2bc056c95 to your computer and use it in GitHub Desktop.
Send email trough Send Grid API with powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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