mirror of
https://github.com/mr-vercetti/bash-scripts.git
synced 2025-07-03 22:05:35 +02:00
Use resend instead of sendgrid for emails
This commit is contained in:
@ -7,7 +7,7 @@ remote_path=""
|
|||||||
email_from=""
|
email_from=""
|
||||||
email_name=""
|
email_name=""
|
||||||
email_to=""
|
email_to=""
|
||||||
send_email_script_path="/etc/sg-send-email.sh"
|
send_email_script_path=""
|
||||||
|
|
||||||
print_usage () {
|
print_usage () {
|
||||||
echo "Usage: "$0" -l LOCAL_PATH -r REMOTE_PATH -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SEND_EMAIL_SCRIPT_PATH"
|
echo "Usage: "$0" -l LOCAL_PATH -r REMOTE_PATH -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SEND_EMAIL_SCRIPT_PATH"
|
||||||
@ -16,7 +16,7 @@ print_usage () {
|
|||||||
echo "EMAIL_FROM - sender's email address"
|
echo "EMAIL_FROM - sender's email address"
|
||||||
echo "EMAIL_NAME - sender's name"
|
echo "EMAIL_NAME - sender's name"
|
||||||
echo "EMAIL_TO - recipient's email adress"
|
echo "EMAIL_TO - recipient's email adress"
|
||||||
echo "SEND_EMAIL_SCRIPT_PATH - a path to the send email script (default: /etc/sg-send-email.sh)"
|
echo "SEND_EMAIL_SCRIPT_PATH - a path to the send email script (default: /etc/send-email.sh)"
|
||||||
}
|
}
|
||||||
|
|
||||||
while getopts "l:r:f:n:t:s:h" opt
|
while getopts "l:r:f:n:t:s:h" opt
|
||||||
@ -62,4 +62,3 @@ if [[ $exit_code -ne 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
${send_email_script_path} -f ${email_from} -n ${email_name} -t ${email_to} -s "Backup sync to S3 status: ${status}" -m "${email_message}"
|
${send_email_script_path} -f ${email_from} -n ${email_name} -t ${email_to} -s "Backup sync to S3 status: ${status}" -m "${email_message}"
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
# Simple script to send email notifications via the Sendgrid API.
|
|
||||||
|
|
||||||
# Default values
|
# Default values
|
||||||
email_from=""
|
email_from=""
|
||||||
@ -7,64 +6,64 @@ email_name=""
|
|||||||
email_to=""
|
email_to=""
|
||||||
subject=""
|
subject=""
|
||||||
message=""
|
message=""
|
||||||
sg_key_file="./sg_key"
|
script_dir="$(dirname $0)"
|
||||||
|
key_file="$script_dir/email_api_key"
|
||||||
|
|
||||||
print_usage () {
|
print_usage () {
|
||||||
echo "Usage: "$0" -f EMAIL_FROM -n EMAIL_NAME -t EMAIL_TO -s SUBJECT -m MESSAGE [-k SG_KEY_FILE]"
|
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_FROM - sender's email address"
|
||||||
echo "EMAIL_NAME - sender's name"
|
echo "EMAIL_NAME - sender's name"
|
||||||
echo "EMAIL_TO - recipient's email adress"
|
echo "EMAIL_TO - recipient's email adress"
|
||||||
echo "SUBJECT - email subject"
|
echo "KEY_FILE - a path to the file with API key (default: ./email_api_key)"
|
||||||
echo "MESSAGE - email message"
|
|
||||||
echo "SG_KEY_FILE - a path to the file with Sendgrid API key (default: ./sg_key)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send_email () {
|
send_email () {
|
||||||
maildata='{"personalizations": [{"to": [{"email": "'${email_to}'"}]}],"from": {"email": "'${email_from}'",
|
maildata='{"personalizations": [{"to": [{"email": "'${email_to}'"}]}],"from": {"email": "'${email_from}'",
|
||||||
"name": "'${email_name}'"},"subject": "'${subject}'","content": [{"type": "text/plain", "value": "'${message}'"}]}'
|
"name": "'${email_name}'"},"subject": "'${subject}'","content": [{"type": "text/plain", "value": "'${message}'"}]}'
|
||||||
|
|
||||||
curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \
|
curl -X "POST" "https://api.resend.com/emails" \
|
||||||
-H "Authorization: Bearer $sg_key" \
|
-H "Authorization: Bearer $api_key" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d "$maildata"
|
-d "$maildata"
|
||||||
}
|
}
|
||||||
|
|
||||||
while getopts "f:n:t:s:m:k:h" opt
|
while getopts "k:t:n:s:m:f:h" opt
|
||||||
do
|
do
|
||||||
case $opt in
|
case $opt in
|
||||||
f)
|
k)
|
||||||
email_from="${OPTARG}";;
|
key_file="${OPTARG}";;
|
||||||
n)
|
|
||||||
email_name="${OPTARG}";;
|
|
||||||
t)
|
t)
|
||||||
email_to="${OPTARG}";;
|
email_to="${OPTARG}";;
|
||||||
|
n)
|
||||||
|
email_name="${OPTARG}";;
|
||||||
s)
|
s)
|
||||||
subject="${OPTARG}";;
|
subject="${OPTARG}";;
|
||||||
m)
|
m)
|
||||||
message="${OPTARG}";;
|
message="${OPTARG}";;
|
||||||
k)
|
f)
|
||||||
sg_key_file="${OPTARG}";;
|
email_from="${OPTARG}";;
|
||||||
h)
|
h)
|
||||||
print_usage
|
print_usage
|
||||||
exit 3
|
exit 0;
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ ! -f $sg_key_file ]]; then
|
if [[ ! -f $key_file ]]; then
|
||||||
echo "API key file \"$sg_key_file\" does not exist"
|
echo "API key file "${OPTARG}" does not exist"
|
||||||
exit 1
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$(stat -c "%a" $sg_key_file)" != "400" ]]; then
|
if [[ "$(stat -c "%a" $key_file)" != "400" ]]; then
|
||||||
echo "Unsafe API key file permissions"
|
echo "Unsafe API key file permissions"
|
||||||
exit 1
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "$(wc -l $sg_key_file | cut -d" " -f1)" != "1" ]]; then
|
if [[ "$(wc -l $key_file | cut -d" " -f1)" != 1 ]]; then
|
||||||
echo "Wrong API key format"
|
echo "Wrong API key file format"
|
||||||
exit 1
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sg_key="$(cat $sg_key_file)"
|
api_key="$(cat $key_file)"
|
||||||
|
|
||||||
send_email
|
send_email
|
Reference in New Issue
Block a user