-
Notifications
You must be signed in to change notification settings - Fork 2
/
bashgmail.sh
executable file
·78 lines (62 loc) · 1.77 KB
/
bashgmail.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
#!/bin/bash
: "
--------------------
Author: Afnan
Date: 03/07/2023
Description:
Allows you to send an email using Gmail's api.
Prior to using this script you need to set up a project on Google's
cloud console turn on gmail's api and then provision a refresh token.
Use this script for only one destination email address.
--------------------
"
if ! which jq > /dev/null;
then
echo ""
echo "Error: jq not installed."
echo "Please install and try again"
echo ""
exit
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
BASE64_COMMAND="base64 -b 0"
else
BASE64_COMMAND="base64 -w 0"
fi
# Set the email content
EMAIL_CONTENT=$(cat ./email_body.txt)
# Set the email address for the recipient
TO_EMAIL=""
# Set the email address for the sender
FROM_EMAIL=""
# Set the email subject
SUBJECT="Automated test"
# Content-Type: text/html; charset=UTF-8
# Construct the email message
EMAIL=$(cat <<EOF
From: ${FROM_EMAIL}
To: ${TO_EMAIL}
Subject: ${SUBJECT}
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
${EMAIL_CONTENT}
EOF
)
# Base64 encode the email message as URL-safe
EMAIL_ENCODED=$(echo -n "${EMAIL}" | base64 -b 0)
# Set the permanent refresh token and client ID/secret
REFRESH_TOKEN=""
CLIENT_ID=""
CLIENT_SECRET=""
# Get a new access token with the refresh token
ACCESS_TOKEN=$(curl -s \
--request POST \
--data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token" \
https://oauth2.googleapis.com/token | jq -r .access_token)
# Send the email using the Gmail API
curl \
--request POST \
--url 'https://gmail.googleapis.com/gmail/v1/users/me/messages/send' \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"raw\":\"$EMAIL_ENCODED\"}"