forked from ambanmba/aws-ses-sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ses-email-sender.sh
115 lines (96 loc) · 3.16 KB
/
ses-email-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
# Aim: Send email with attachment from AWS SES
# Coder : [email protected] / Batur Orkun
# Modifications by Alastair Bor
# Fixed typos / non-working syntax
# Removed setting aws region/id/key which can be done separately
# Rewritten to allow for larger attachments (AWS limit is 10MB)
# Removed the need to have a separate template file
# Added automatic determination of correct MIME Type for attachment
# Modifications by Luan Paschoal
# charset utf-8 para acentos
# uso direto de variaveis sem comando de replace (sed)
# geração de arquivo tmp-message.txt para analisar msg antes de envio
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 ]
" 1>&2;
exit 1;
}
function Error() {
echo "Error: $1"
exit
}
function checkRequirements() {
which aws
if [ $? -ne 0 ]; then
Error "AWS cli tool is not installed"
fi
which base64
if [ $? -ne 0 ]; then
Error "base64 tool is not installed"
fi
}
function sendMail() {
if [[ -z ${ATTACHMENT} ]]; then
ATTACHMENT=$BODY
FILENAME="Message.txt"
MIMETYPE="text/plain"
else
FILENAME=$(basename "${ATTACHMENT}")
MIMETYPE=`file --mime-type "$ATTACHMENT" | sed 's/.*: //'`
ATTACHMENT=`base64 "$ATTACHMENT"`
fi
PARTA="{\"Data\": \"From: $FROM\nTo: $RECVS\nSubject: {SUBJECT}\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\\\"NextPart\\\"\\n\\n--NextPart\\nContent-Type: text/plain; charset=utf-8\\nContent-Transfer-Encoding: base64\\n\\n$BODY\\n\\n--NextPart\\nContent-Type: $MIMETYPE;\\nContent-Disposition: attachment; filename=\\\"$FILENAME\\\"\\nContent-Transfer-Encoding: base64\\n\\n"
PARTB="\\n\\n--NextPart--\"}"
TMPFILE="/tmp/ses-$(date +%s)"
printf "%s" $PARTA > $TMPFILE
printf "%s" $ATTACHMENT >> $TMPFILE
printf "%s" $PARTB >> $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!{MIMETYPE}!$MIMETYPE!g" $TMPFILE #Use ! as delimiter because MIMETYPE has /
#sed -i '' -e "s/$(printf '\r')//g" $TMPFILE #Remove extraneous \r characters
echo "$(cat $TMPFILE)" > tmp-message.txt
aws ses send-raw-email --cli-binary-format raw-in-base64-out --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
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
checkRequirements
sendMail