-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsendgridSendEmail.sh
executable file
·137 lines (108 loc) · 3.27 KB
/
sendgridSendEmail.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Author: Amith Chandrappa
# Usage
# Options:
# -t: To Emails, Separated by ";"
# -c: CC Emails, Separated by ";"
# -b: BCC Emails, Separated by ";"
# -s: Subject
# -o: Email body
# -a: Attachment Files, Separated by ";"
#
# Example:
# sh sendEmail.sh
# -t 'to1@gmail.com;to2@gmail.com'
# -c 'cc1@gmail.com;cc2@gmail.com'
# -b 'bcc1@gmail.com;bcc2@gmail.com'
# -s 'FINAL SCRIPT'
# -o '<p>Email body goes here</p>'
# -a '/tmp/test.sh;/tmp/test2.sh'
# Your Sendgrid API Key
#SENDGRID_API_KEY=""
#FROM_NAME=""
#FROM_EMAIL=""
# Get the arguments
while getopts t:c:b:s:o:a: flag
do
case "${flag}" in
t) to=${OPTARG};;
c) cc=${OPTARG};;
b) bcc=${OPTARG};;
s) subject=${OPTARG};;
o) body=${OPTARG};;
a) attachments=${OPTARG};;
esac
done
# Start building the JSON
sendGridJson="{\"personalizations\": [{";
# Convert the String to Array, with the delimiter as ";"
IFS='; ' read -r -a to_array <<< "$to"
IFS='; ' read -r -a cc_array <<< "$cc"
IFS='; ' read -r -a bcc_array <<< "$bcc"
IFS='; ' read -r -a attachments_array <<< "$attachments"
if [ ${#to_array[@]} != 0 ]
then
sendGridJson="${sendGridJson} \"to\": ["
for email in "${to_array[@]}"
do
sendGridJson="${sendGridJson} {\"email\": \"$email\"},"
done
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
sendGridJson="${sendGridJson} ],"
if [ ${#cc_array[@]} == 0 ] && [ ${#bcc_array[@]} == 0 ]
then
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
fi
fi
if [ ${#cc_array[@]} != 0 ]
then
sendGridJson="${sendGridJson} \"cc\": ["
for email in "${cc_array[@]}"
do
sendGridJson="${sendGridJson} {\"email\": \"$email\"},"
done
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
sendGridJson="${sendGridJson} ],"
if [ ${#bcc_array[@]} == 0 ]
then
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
fi
fi
if [ ${#bcc_array[@]} != 0 ]
then
sendGridJson="${sendGridJson} \"bcc\": ["
for email in "${bcc_array[@]}"
do
sendGridJson="${sendGridJson} {\"email\": \"$email\"},"
done
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
sendGridJson="${sendGridJson} ]"
fi
sendGridJson="${sendGridJson} }],\"from\": {\"email\": \"${FROM_EMAIL}\",\"name\": \"${FROM_NAME}\"},\"subject\":\"${subject}\",\"content\": [{\"type\": \"text/html\",\"value\": \"${body}\"}],"
if [ ${#attachments_array[@]} != 0 ]
then
sendGridJson="${sendGridJson} \"attachments\": ["
for attachment in "${attachments_array[@]}"
# Converting the File Content to Base64
# For OSX use base64 <fileName>
# For linux use base64 -w 0 < <fileName>
do
base64_content=$(base64 -w 0 < ${attachment})
fileName="$(basename $attachment)"
sendGridJson="${sendGridJson} {\"content\": \"${base64_content}\",\"type\": \"text/plain\",\"filename\": \"${fileName}\"},"
done
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
sendGridJson="${sendGridJson} ]"
else
sendGridJson=`echo ${sendGridJson} | sed 's/.$//'`
fi
sendGridJson="${sendGridJson} }"
#Generate a Random File to hole the POST data
tfile=$(mktemp /tmp/sendgrid.XXXXXXXXX)
echo $sendGridJson > $tfile
# Send the http request to SendGrid
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer '$SENDGRID_API_KEY \
--header 'Content-Type: application/json' \
--data @$tfile