Last active
January 16, 2025 01:21
-
-
Save georgeOsdDev/4ca90efc5d8e918be5a7a627edb54435 to your computer and use it in GitHub Desktop.
Azure Communication Service Email sending by curl
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
#!/bin/sh | |
set -Ceu | |
# ACS Email API | |
# https://learn.microsoft.com/en-us/rest/api/communication/email/email/send?view=rest-communication-email-2023-03-31&tabs=HTTP | |
# How to sign the request | |
# https://learn.microsoft.com/en-us/rest/api/communication/authentication#signing-an-http-request | |
# Common headers | |
# https://learn.microsoft.com/en-us/rest/api/communication/headers | |
# Repeatability of requests | |
# https://learn.microsoft.com/en-us/rest/api/communication/repeatable-requests | |
# See also | |
# https://learn.microsoft.com/en-us/azure/azure-app-configuration/rest-api-authentication-hmac#bash | |
# https://stackoverflow.com/questions/20103258/accessing-azure-blob-storage-using-bash-curl | |
# Parameters from ACS resource | |
######################################## | |
# https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email?tabs=windows%2Cconnection-string%2Csend-email-and-get-status-async%2Csync-client&pivots=platform-azportal | |
# Replace <YourSecret> with actual secret from ACS resource. | |
secret="<YourSecret>" | |
# secret="7W9ichYxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
# Replace <YourHost> with actual endpoint host from ACS resource. Eg: <ResourceName>.<region>.communication.azure.com | |
host="<YourHost>" | |
# host="xxxxxxxxxxxxxxxxxxxxxxx.japan.communication.azure.com" | |
# Replace <YourMailFromAddress> with the MailFrom address of your verified domain. Eg: [email protected] | |
senderAddress="<YourMailFromAddress>" | |
# senderAddress="[email protected]" | |
# Replace <RecipientAddress> with the email address you would like to send a message to. | |
recipientAddress="<RecipientAddress>" | |
# recipientAddress="[email protected]" | |
######################################## | |
body=$(cat <<EOF | |
{ | |
"Recipients": { | |
"To": [ | |
{ | |
"DisplayName": "", | |
"address": "${recipientAddress}" | |
} | |
] | |
}, | |
"Content": { | |
"Subject": "ACS mail from shell script with curl", | |
"PlainText": "こんにちは💛" | |
}, | |
"SenderAddress": "${senderAddress}" | |
} | |
EOF | |
) | |
path="/emails:send?api-version=2023-03-31" | |
method="POST" | |
uuid=$(uuidgen) | |
gmt=$(date -u +"%a, %d %b %Y %H:%M:%S GMT") | |
contentHash=$(echo -n "$body" | openssl dgst -sha256 -binary | base64 -w0) | |
# echo $contentHash | |
stringToSign="${method} | |
${path} | |
${gmt};${host};${contentHash}" | |
# echo -n "${stringToSign}" | |
decodedHexkey="$(echo -n $secret | base64 -d -w0 | xxd -p -c256)" | |
signature=$(echo -n "${stringToSign}" | openssl dgst -binary -sha256 -mac HMAC -macopt "hexkey:${decodedHexkey}" | base64 -w0) | |
# echo -n $signature | |
curl -X $method "https://$host$path" \ | |
-H "x-ms-content-sha256: ${contentHash}" \ | |
-H "Authorization: HMAC-SHA256 SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=$signature" \ | |
-H "x-ms-date: $gmt" \ | |
-H "Content-Type: application/json" \ | |
-H "repeatability-first-sent: $gmt" \ | |
-H "repeatability-request-id: $uuid" \ | |
-d "$body" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment