Skip to content

Instantly share code, notes, and snippets.

@goofmint
Last active Nov 25, 2020
Embed
What would you like to do?
コマンドラインでCustomers Mail Cloudからメール送信を行うシェルスクリプト
#!/bin/bash -x
PROGNAME=$(basename $0)
VERSION='2.0'
declare -a FILES=()
usage() {
echo "Usage: $PROGNAME [OPTIONS]"
echo " This script is sending email from shell script by Customers Mail Cloud."
echo
echo "Options:"
echo " -h, --help"
echo " --version"
echo " -c, --contract trial | standard | subdmain"
echo " -u, --api-user API_USER"
echo " -k, --api-key API_KEY"
echo " -e, --email to@example.com"
echo " -n, --name EMAIL_NAME"
echo " -f, --from from@example.com"
echo " -s, --subject EMAIL_SUBJECT"
echo " -t, --text File path"
echo " -fn, --from-name EMAIL_FROM_NAME"
echo " -F, --file File path"
echo
exit 1
}
escape () {
printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}
file_to_jsonparam (){
IFS=$'\n'
OUTPUT="/tmp/file_to_jsonparam"
echo "" > ${OUTPUT}
cat $1 | while read LINE
do
ESCAPED_LINE=`echo $LINE | sed -e "s/\([\&\:\"\<\>\{\}\/]\)/__ESCAPE__\1/g" | sed -e "s/__ESCAPE__/\\\\\/g"`
echo -n "${ESCAPED_LINE}\\r\\n" >> ${OUTPUT}
done
RESULT=`cat ${OUTPUT}`
rm -f ${OUTPUT}
echo ${RESULT}
}
for OPT in "$@"
do
case $OPT in
-h | --help)
usage
exit 1
;;
--version)
echo $VERSION
exit 1
;;
-c | --contract)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
CONTRACT=$2
shift 2
;;
-k | --api-key)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
API_KEY=`escape $2`
shift 2
;;
-u | --api-user)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
API_USER=`escape $2`
shift 2
;;
-s | --subject)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
SUBJECT=`escape $2`
shift 2
;;
-e | --email)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
EMAIL=`escape $2`
shift 2
;;
-n | --name)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
NAME=`escape $2`
shift 2
;;
-f | --from)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
FROM=`escape $2`
shift 2
;;
-fn | --from-name)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
FROM_NAME=`escape $2`
shift 2
;;
-t | --text)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
TEXT=$2
shift 2
;;
-F | --file)
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME: option requires an argument -- $1" 1>&2
exit 1
fi
FILES+=($2)
shift 2
;;
-*)
echo "$PROGNAME: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
exit 1
;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
param+=( "$1" )
shift 1
fi
;;
esac
done
if [ "$FROM_NAME" = '' ]; then
FROM_NAME="\"\""
fi
if [ "$NAME" = '' ]; then
NAME="\"\""
fi
if [ "$TEXT" = '' ]; then
echo "$PROGNAME: Text is required. Using --text or -t [filename]." 1>&2
exit 1
else
BODY=`file_to_jsonparam $TEXT`
fi
if [ "$SUBJECT" = '' ]; then
echo "$PROGNAME: Subject is required. Using --subject or -s subject." 1>&2
exit 1
fi
if [ "$EMAIL" = '' ]; then
echo "$PROGNAME: Email address is required. Using --email or -e email@exapmle.com." 1>&2
exit 1
fi
if [ "$FROM" = '' ]; then
echo "$PROGNAME: From email address is required. Using --from or -f email@exapmle.com." 1>&2
exit 1
fi
if [ "$CONTRACT" = '' ]; then
echo "$PROGNAME: Your contract is required. Using --contract or -c trial | standard | subdmain(pro) ." 1>&2
exit 1
fi
if [ "$API_USER" = '' ]; then
echo "$PROGNAME: API User is required. Using --api-user or -u YOUR_USER." 1>&2
exit 1
fi
if [ "$API_KEY" = '' ]; then
echo "$PROGNAME: API Key is required. Using --api-key or -k YOUR_KEY." 1>&2
exit 1
fi
if [ "$CONTRACT" = 'trial' ]; then
URL='https://sandbox.smtps.jp/api/v2/emails/send.json'
elif [ "$CONTRACT" = 'standard' ]; then
URL='https://te.smtps.jp/api/v2/emails/send.json'
else
URL="https://$CONTRACT.smtps.jp/api/v2/emails/send.json"
fi
# Make JSON
# echo $TEXT
echo `escape $SUBJECT`
if [ "${#FILES[@]}" = '0' ]; then
json=$(cat << EOS
{
"api_user" : $API_USER,
"api_key" : $API_KEY,
"to" : [
{
"name" : $NAME,
"address" : $EMAIL
}
],
"from" : {
"name" : $FROM_NAME,
"address" : $FROM
},
"subject" : $SUBJECT,
"text" : "$BODY"
}
EOS
)
curl -X POST -H "Content-Type: application/json" \
-d "$json" $URL
else
ARG = ""
i=1
for e in ${FILES[@]}; do
echo "attachment$i = ${e}"
ARG="$ARG -F attachment$i=@$e"
let i++
done
curl -X POST \
-F "api_user=$API_USER" \
-F "api_key=$API_KEY" \
-F "to={"name": "$NAME","address": "$EMAIL"}" \
-F "from={"name": "$FROM_NAME","address":"$FROM"}" \
-F "attachments=${#FILES[@]}" \
-F "subject=$SUBJECT" \
--form-string "text=$(<$TEXT)" \
$ARG \
$URL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment