From 713ab58769c57428bb5cb440824081210575bfb0 Mon Sep 17 00:00:00 2001 From: mr-vercetti <87.milewski@gmail.com> Date: Sat, 21 Jun 2025 14:23:49 +0200 Subject: [PATCH] Use resend instead of sendgrid for emails --- s3-sync-backup.sh | 5 ++- sg-send-email.sh => send-email.sh | 55 +++++++++++++++---------------- 2 files changed, 29 insertions(+), 31 deletions(-) rename sg-send-email.sh => send-email.sh (53%) diff --git a/s3-sync-backup.sh b/s3-sync-backup.sh index 98d1476..77bc51c 100755 --- a/s3-sync-backup.sh +++ b/s3-sync-backup.sh @@ -7,7 +7,7 @@ remote_path="" email_from="" email_name="" email_to="" -send_email_script_path="/etc/sg-send-email.sh" +send_email_script_path="" 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" @@ -16,7 +16,7 @@ print_usage () { echo "EMAIL_FROM - sender's email address" echo "EMAIL_NAME - sender's name" 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 @@ -62,4 +62,3 @@ if [[ $exit_code -ne 0 ]]; then fi ${send_email_script_path} -f ${email_from} -n ${email_name} -t ${email_to} -s "Backup sync to S3 status: ${status}" -m "${email_message}" - diff --git a/sg-send-email.sh b/send-email.sh similarity index 53% rename from sg-send-email.sh rename to send-email.sh index 675f0a7..7c7a661 100755 --- a/sg-send-email.sh +++ b/send-email.sh @@ -1,5 +1,4 @@ -#!/usr/bin/bash -# Simple script to send email notifications via the Sendgrid API. +#!/usr/bin/bash # Default values email_from="" @@ -7,64 +6,64 @@ email_name="" email_to="" subject="" message="" -sg_key_file="./sg_key" +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 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_NAME - sender's name" echo "EMAIL_TO - recipient's email adress" - echo "SUBJECT - email subject" - echo "MESSAGE - email message" - echo "SG_KEY_FILE - a path to the file with Sendgrid API key (default: ./sg_key)" + 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.sendgrid.com/v3/mail/send" \ - -H "Authorization: Bearer $sg_key" \ + curl -X "POST" "https://api.resend.com/emails" \ + -H "Authorization: Bearer $api_key" \ -H "Content-Type: application/json" \ -d "$maildata" } -while getopts "f:n:t:s:m:k:h" opt +while getopts "k:t:n:s:m:f:h" opt do case $opt in - f) - email_from="${OPTARG}";; - n) - email_name="${OPTARG}";; + k) + key_file="${OPTARG}";; t) email_to="${OPTARG}";; + n) + email_name="${OPTARG}";; s) subject="${OPTARG}";; m) message="${OPTARG}";; - k) - sg_key_file="${OPTARG}";; + f) + email_from="${OPTARG}";; h) - print_usage - exit 3 - ;; + print_usage + exit 0; + ;; esac done -if [[ ! -f $sg_key_file ]]; then - echo "API key file \"$sg_key_file\" does not exist" - exit 1 +if [[ ! -f $key_file ]]; then + echo "API key file "${OPTARG}" does not exist" + exit 1; fi -if [[ "$(stat -c "%a" $sg_key_file)" != "400" ]]; then +if [[ "$(stat -c "%a" $key_file)" != "400" ]]; then echo "Unsafe API key file permissions" - exit 1 + exit 1; fi -if [[ "$(wc -l $sg_key_file | cut -d" " -f1)" != "1" ]]; then - echo "Wrong API key format" - exit 1 +if [[ "$(wc -l $key_file | cut -d" " -f1)" != 1 ]]; then + echo "Wrong API key file format" + exit 1; fi -sg_key="$(cat $sg_key_file)" +api_key="$(cat $key_file)" + send_email