mirror of
				https://github.com/mr-vercetti/bash-scripts.git
				synced 2025-11-04 09:25:48 +01:00 
			
		
		
		
	Use resend instead of sendgrid for emails
This commit is contained in:
		@@ -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}"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
#!/usr/bin/bash
 | 
			
		||||
# Simple script to send email notifications via the Sendgrid API.
 | 
			
		||||
 | 
			
		||||
# 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
 | 
			
		||||
         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
 | 
			
		||||
		Reference in New Issue
	
	Block a user