Skip to content

Instantly share code, notes, and snippets.

@kishea
Created February 17, 2022 02:00
Show Gist options
  • Save kishea/25d8b5763ce2abfe9c5f461601f45839 to your computer and use it in GitHub Desktop.
Save kishea/25d8b5763ce2abfe9c5f461601f45839 to your computer and use it in GitHub Desktop.
Shell script to send email via amazon ses and also upload a zip file amazon S3
#!/bin/bash
# CONSTANTS
AWS_KEY="AWSKEY"
AWS_SECRET="U+CDjyzDPnBn/E//KEYPPP+oXkhIuYChCbbIW"
REGION=us-west-2
S3_BUCKET="s3buckeet"
S3_BUCKET_PATH="/"
S3_ACL="x-amz-acl:private"
AWS_SVR_DATE=$(date +"%a, %d %b %Y %T %z")
MAIL_GUN_DOMAIN="mail.chapchap.info"
MAILGUN_API_KEY="AG-3d0809fb-f7c17087"
function s3Upload() {
path=$1
file=$2
acl=${S3_ACL}
bucket=${S3_BUCKET}
bucket_path=${S3_BUCKET_PATH}
# date=$(date +"%d-%m-%Y-%H%M")
content_type="application/octet-stream"
sig_string="PUT\n\n$content_type\n$AWS_SVR_DATE\n$acl\n/$bucket$bucket_path$file"
echo "\n"
echo "$bucket_path$file"
echo "\n"
signature=$(echo -en "${sig_string}" | openssl sha1 -hmac "${AWS_SECRET}" -binary | base64)
curl -X PUT -T "$path" \
-H "Host: $bucket.s3.amazonaws.com" \
-H "Date: $AWS_SVR_DATE" \
-H "Content-Type: $content_type" \
-H "$acl" \
-H "Authorization: AWS ${AWS_KEY}:$signature" \
"https://$bucket.s3.amazonaws.com$bucket_path$file"
}
function send_mailgun() {
echo "===========\nTO: $1\nSUBJECT: $2\nTEXT: $3\nHTML: $4\n========\n"
curl -s --user "api:$MAILGUN_API_KEY" \
"https://api.mailgun.net/v3/$MAIL_GUN_DOMAIN/messages" \
-F from='CHAPCHAP <no-reply@chapchap.co>' \
-F to=$1 \
-F subject=$2 \
-F html="$3"
}
function check_aws_cli_installed() {
if which aws >/dev/null; then
# echo "PROCEEDING with aws cli"
# echo "set environmrnt variables"
export AWS_ACCESS_KEY_ID=$AWS_KEY
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET
export AWS_DEFAULT_REGION=$REGION
else
echo "aws cli does not exist"
echo "installing aws cli"
if [[ $OSTYPE == 'darwin'* ]]; then
echo 'macOS'
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg ./AWSCLIV2.pkg -target /
fi
if [[ $OSTYPE == 'linux'* ]]; then
echo "LINUX"
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
fi
fi
}
function send_ses() {
check_aws_cli_installed
AWS_CLI=$(which aws)
string=$@
echo ""
echo ""
echo $string
readarray -t array < <(tr '^^^' '\n' <<<"$string" | sed 's/^ *//' | sed 's/ *$//')
declare -p array
echo "${array[@]}"
SENDER="${array[0]} <${array[3]}>"
RECIPIENT="${array[6]}"
SUBJECT="${array[9]}"
BODY=$(python3 -c "import html,sys; print(html.unescape('${array[12]}'), end='')")
# BODY=$(echo "$BODY" | sed -e 's/#\!/ /g')
BODY=$(echo "$BODY" | sed -e 's/"/\\"/g')
BODY=$(echo "$BODY" | sed -e 's/<!DOCTYPE html>//')
BODY=$(echo \"$BODY\" | tr -d '\n')
UUID="$HOME/messages.log"
echo "============================= "
echo "FROM : $SENDER "
echo "TO : $RECIPIENT "
echo "SUBJECT : $SUBJECT "
echo "BODY : $BODY "
echo "============================= "
if [ -f "$AWS_CLI" ]; then
VAR1=$RECIPIENT
echo "SND TO $RECIPIENT"
str1="$RECIPIENT"
str2="mkishea12@gmail.com"
if [ "$str1" == "$str2" ]; then
aws ses send-email \
--region "$REGION" \
--from "$SENDER" \
--destination "ToAddresses=$RECIPIENT,BccAddresses=support@kteqapps.com" \
--message "Subject={Data=$SUBJECT,Charset=utf-8},Body={Html={Data=$BODY,Charset=utf-8}}" >> \
$UUID
fi
else
echo "WARNING: Skipping sending email. AWS CLI not found in path. This should be okay if the script doesn't run in the docker image."
fi
# rm $UUID;
}
# function send_ses_with_attachment() {
# echo "Sending file via email..."
# AWS_CLI="/usr/bin/aws"
# SENDER="ChapChap Africa <support@chapchap.co>"
# RECIPIENT=$1
# SUBJECT=$2
# BODY=$3
# UUID = "${uuidgen}-message.json"
# # ATTACHMENT_TYPE="text/plain"
# # ATTACHMENT_FILE_NAME="report.csv"
# # ATTACHMENT_FILE_TO_READ_FROM_DISK="*.csv"
# echo UUID;
# # Create message
# echo '{"Data": "From:'${SENDER}'\nTo:'${RECIPIENT}'\nSubject:'${SUBJECT}'\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\n['${BODY}']\n\n--NextPart\nContent-Type:'${ATTACHMENT_TYPE}';\nContent-Disposition: attachment; filename=\"'${ATTACHMENT_FILE_NAME}'\"\n\n'$(cat ./${ATTACHMENT_FILE_TO_READ_FROM_DISK})'\n--NextPart--"}' > "${UUID}"
# # if [-f "$4"]
# # echo '{"Data": "From:'${SENDER}'\nTo:'${RECIPIENT}'\nSubject:'${SUBJECT}'\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\n['${BODY}']\n\n--NextPart\n"}' > "${UUID}"
# if [ -f "$AWS_CLI" ]; then
# $AWS_CLI ses send-raw-email --bcc "kteqapps@gmail.com" --reply-to "no-reply@chapchap.co" --region eu-west-2 --raw-message file://"${UUID}"
# else
# echo "WARNING: Skipping sending email. AWS CLI not found in path. This should be okay if the script doesn't run in the docker image."
# fi
# rm UUID;
# }
send_ses $1
@kishea
Copy link
Author

kishea commented Feb 17, 2022

Test with

./send-email.sh 'chapchap^^^support@chapchap.co^^^someone@gmail.com^^^Chapchap Password Reset^^^<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"UTF-8\">\n<title>ChapChap Africa</title>\n</head>\n<style>\n</style>\n<body>\n\n<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 500px; margin-left: auto; margin-right: auto; font-family: arial,sans-serif;\">\n<thead style=\"border-bottom: solid 1px gray; color:#7c7c7c; font-weight: 100;\">\n<tr>\n<th>\n<a href=\"https://chapchap.co\"><img src=\"https://chapchap.co/img/logo.png\" height=\"60px\" style=\"float: left\">\n<span style=\"color:#f44336; float: left; font-size: 40px;\">ChapChap</span></a>\n</th>\n<th></th>\n<th><h2 style=\"width: 200px; float: right;\">Password Reset</h2></th>\n</tr>\n<tr>\n<th colspan=\"3\"><hr></th>\n</tr>\n</thead>\n\n<tbody style=\"color:#4f4f4f; font-weight: 100; \">\n<tr>\n<td colspan=\"3\"><p>Click link below to reset your password</p><p><a href="https://www.chapchap.co/app/#!/reset-password/5787771516459268912928">https://www.chapchap.co/app/#!/reset-password/5787771516459268912928</a></p></td>\n</tr>\n<tr>\n<td colspan=\"3\">\nThank You For using Chapchap\n</td>\n</tr>\n<tr style=\"background-color: #fcfcfc; color: #878787; font-size: 10px; padding-top: 10px;\">\n<td colspan=\"3\" style=\"padding-bottom: 10px; padding-top: 10px;\">\nThis email was sent from <a href=\"http://chapchap.co\">ChapChap Africa</a>\n<br>\nThis email was sent from a notification-only address that cannot accept incoming email. Please do not reply to this message.\n</td>\n</tr>\n</tbody>\n</table>\n\n</body>\n</html>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment