-
Notifications
You must be signed in to change notification settings - Fork 13
/
sender.sh
115 lines (96 loc) · 2.34 KB
/
sender.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# Created on 2019.08.10
# Update on 2019.08.10 23pm
# Aim: Send email with attachment from AWS SES
# Coder : [email protected] / Batur Orkun
## Global vars
function usage() {
echo "Usage: $0 [-h|--help ]
[-s|--subject <string> subject/title for email ]
[-f|--from <email> ]
[-r|--receiver|--receivers <emails> coma seperated emails ]
[-b|--body <string> ]
[-a|--attachment <filename> filepath ]
[--aws-region <string> Change Default AWS Region ]
[--aws_access_key_id <string> Change AWS Access Key ID ]
[--aws_secret_access_key <string> Change AWS Secret Access Key ]
" 1>&2;
exit 1;
}
function Error() {
echo "Error: $1"
exit
}
function checkRequirements() {
which aws
if [ $? -ne 0 ]; then
Error "AWS Cli tool is installed"
fi
which base64
if [ $? -ne 0 ]; then
Error "base64 tool is installed"
fi
}
function sendMail() {
if [[ -z ${ATTACHMENT} ]]; then
ATTACHMENT=$BODY
FILENAME="Message.txt"
else
FILENAME=$(basename "${ATTACHMENT%}")
ATTACHMENT=`base64 -i $ATTACHMENT`
fi
TEMPLATE="ses-email-template.json"
TMPFILE="/tmp/ses-$(date +%s)"
cp $TEMPLATE $TMPFILE
sed -i -e "s/{SUBJECT}/$SUBJECT/g" $TMPFILE
sed -i -e "s/{FROM}/$FROM/g" $TMPFILE
sed -i -e "s/{RECVS}/$RECVS/g" $TMPFILE
sed -i -e "s/{BODY}/$BODY/g" $TMPFILE
sed -i -e "s/{FILENAME}/$FILENAME/g" $TMPFILE
sed -i -e "s/{ATTACHMENT}/$ATTACHMENT/g" $TMPFILE
aws ses send-raw-email --raw-message file://$TMPFILE
}
while :; do
case $1 in
-h|-\?|--help)
usage
;;
-s|--subject)
SUBJECT=$2
shift
;;
-f|--from)
FROM=$2
shift
;;
-r|--receiver|--receivers)
RECVS=$2
shift
;;
-b|--body)
BODY=`echo "$2" | base64`
shift
;;
-a|--attachment)
ATTACHMENT=$2
shift
;;
--aws-region)
AWS_DEFAULT_REGION=$2
shift
;;
--aws_access_key_id)
AWS_ACCESS_KEY_ID=$2
shift
;;
--aws_secret_access_key)
AWS_SECRET_ACCESS_KEY=$2
shift
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
checkRequirements
sendMail