#!/usr/bin/bash # Default values email_from="" email_name="" email_to="" subject="" message="" script_dir="$(dirname $0)" key_file="$script_dir/email_api_key" print_usage () { echo "Usage: "$0" -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE [-k KEY_FILE]" echo "EMAIL_FROM - sender's email address" echo "EMAIL_NAME - sender's name" echo "EMAIL_TO - recipient's email adress" echo "KEY_FILE - a path to the file with API key (default: ./email_api_key)" } send_email () { maildata='{"personalizations": [{"to": [{"email": "'${email_to}'"}]}],"from": {"email": "'${email_from}'", "name": "'${email_name}'"},"subject": "'${subject}'","content": [{"type": "text/plain", "value": "'${message}'"}]}' curl -X "POST" "https://api.resend.com/emails" \ -H "Authorization: Bearer $api_key" \ -H "Content-Type: application/json" \ -d "$maildata" } while getopts "k:t:n:s:m:f:h" opt do case $opt in k) key_file="${OPTARG}";; t) email_to="${OPTARG}";; n) email_name="${OPTARG}";; s) subject="${OPTARG}";; m) message="${OPTARG}";; f) email_from="${OPTARG}";; h) print_usage exit 0; ;; esac done if [[ ! -f $key_file ]]; then echo "API key file "${OPTARG}" does not exist" exit 1; fi if [[ "$(stat -c "%a" $key_file)" != "400" ]]; then echo "Unsafe API key file permissions" exit 1; fi if [[ "$(wc -l $key_file | cut -d" " -f1)" != 1 ]]; then echo "Wrong API key file format" exit 1; fi api_key="$(cat $key_file)" send_email