Skip to content

Instantly share code, notes, and snippets.

@broland07
Created January 25, 2021 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save broland07/acd1ed48821335d4de81fa1daf047543 to your computer and use it in GitHub Desktop.
Save broland07/acd1ed48821335d4de81fa1daf047543 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Email setup
SENDGRID_API_KEY="SG.xxxxxx"
FILENAME_ATTACH="bash test file"
FILENAME_ZIP="test.txt"
FILENAME_BASE64_TMP="test.txt"
EMAIL_TO="to@example.com"
EMAIL_SUBJECT="test bash file"
EMAIL_FROM="from@example.com"
EMAIL_MESSAGE="test bash file"
function email_exports()
{
FILENAME_BASE64=$(base64 -w0 $FILENAME_ZIP);
REQUEST_DATA='{"personalizations": [{
"to": [{ "email": "'"$EMAIL_TO"'" }],
"subject": "'"$EMAIL_SUBJECT"'"
}],
"from": {
"email": "'"$EMAIL_FROM"'"
},
"content": [{
"type": "text/plain",
"value": "'"$EMAIL_MESSAGE"'"
}],
"attachments": [{
"content": "'"$FILENAME_BASE64"'",
"filename": "'"$FILENAME_ATTACH"'"
}]
}';
# We need to store the base64 locally as the text
# is too big for sending directly with curl
echo $REQUEST_DATA > $FILENAME_BASE64_TMP
curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \
-H "Authorization: Bearer $SENDGRID_API_KEY" \
-H "Content-Type: application/json" \
-d "@$FILENAME_BASE64_TMP";
rm $FILENAME_BASE64_TMP
}
email_exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment