Open1

SendGridからPowershellでメールを送る

kiwikiwi

SendGridのAPIのドキュメント

サンプルコード

$SENDGRID_API_ENDPOINT = "https://api.sendgrid.com/v3/mail/send"

$SENDGRID_API_KEY = "<SendGrid の API キー>"
$to = "<送信先メールアドレス>"
$from = "<送信元メールアドレス>" # Sender Authentication を設定したもの

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Bearer $SENDGRID_API_KEY")

$body = "{ `"personalizations`": [ { `"to`": [ { `"email`": `"$to`" } ], `"subject`": `"Hello, World!`" } ], `"from`": { `"email`": `"$from`" }, `"content`": [ { `"type`": `"text/plain`", `"value`": `"Hello, World!`" } ]}"
$body_utf8 = [System.Text.Encoding]::UTF8.GetBytes($body)

$response = Invoke-RestMethod $SENDGRID_API_ENDPOINT -Method "POST" -Headers $headers -Body $body_utf8