-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail.sh
executable file
·165 lines (143 loc) · 3.96 KB
/
sendmail.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
#
# Send anonymous email from terminal
#
#/ Usage:
#/ ./sendmail.sh -t <to_address> [-s <subject>|-m <message>]
#/
#/ Options:
#/ -t required, recipient mail address
#/ -s optional, subject
#/ -m optional, message
#/ -h | --help display this help message
set -e
set -u
usage() {
printf "%b\n" "$(grep '^#/' "$0" | cut -c4-)" >&2 && exit 0
}
set_var() {
_TMP_FILE=$(mktemp)
_HOST="https://tempr.email"
_API="${_HOST}/application/api"
_SID="$(tr -dc 'a-z0-9' < /dev/urandom | head -c26)"
_COOKIE="LocalPart=${_SID}; DomainId=1; sid=${_SID}"
_FROM_ADDRESS="${_SID}@tempr.email"
}
set_command() {
_CURL="$(command -v curl)" || command_not_found "curl" "https://curl.haxx.se/download.html"
_VIU="$(command -v viu)" || command_not_found "viu" "https://github.com/atanunq/viu"
}
set_args() {
expr "$*" : ".*--help" > /dev/null && usage
_MAIL_MESSAGE="$(tr -dc 'a-z0-9' < /dev/urandom | head -c128)"
_MAIL_SUBJECT="$(tr -dc 'a-z0-9' < /dev/urandom | head -c16)"
while getopts ":ht:s:m:" opt; do
case $opt in
t)
_TO_ADDRESS="$OPTARG"
;;
s)
_MAIL_SUBJECT="$OPTARG"
;;
m)
_MAIL_MESSAGE="$OPTARG"
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
}
print_info() {
# $1: info message
printf "%b\n" "\033[32m[INFO]\033[0m $1" >&2
}
print_error() {
# $1: error message
printf "%b\n" "\033[31m[ERROR]\033[0m $1" >&2
exit 1
}
command_not_found() {
# $1: command name
# $2: installation URL
if [[ -n "${2:-}" ]]; then
print_error "$1 command not found! Install from $2"
else
print_error "$1 command not found!"
fi
}
check_var() {
if [[ -z "${_TO_ADDRESS:-}" ]]; then
print_error "Missing recipient email address: -t <to_address>"
fi
}
cleanup() {
if [[ -n "${_TMP_FILE:-}" && -f "$_TMP_FILE" ]]; then
rm -f "$_TMP_FILE"
fi
}
encode_url() {
# $1: input string
# code from https://stackoverflow.com/a/10660730
local string="$1"
local strlen=${#string}
local encoded pos c o
encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02X' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
account_login() {
$_CURL -sS "${_HOST}/en/" \
-H "Cookie: ${_COOKIE}" \
--data-raw "LocalPart=${_SID}&DomainType=public&DomainId=1&PrivateDomain=&Password=&LoginButton=Log+in+%26+check+e-mails&CopyAndPaste=${_FROM_ADDRESS}" \
&> /dev/null
}
enter_captcha() {
local c=""
$_CURL -sS "${_API}/secureCaptcha.php?sid=${_SID}&small=1" \
-H "Cookie: ${_COOKIE}" \
-o "$_TMP_FILE"
[[ ! -s "$_TMP_FILE" ]] && print_error "Failed to fetch Captcha image"
$_VIU "$_TMP_FILE" >&2
while [[ "$c" == "" ]]; do
read -rp "Enter captcha letters (case insensitive): " c
done
echo "$c"
}
send_mail() {
local m s c s
m="$(encode_url "$_MAIL_MESSAGE")"
s="$(encode_url "$_MAIL_SUBJECT")"
c="$(enter_captcha)"
s=$($_CURL -sS "${_HOST}/en/sendmail.htm" -H "Cookie: ${_COOKIE}" --data-raw "FromAddress=${_FROM_ADDRESS}&ToAddress=${_TO_ADDRESS}&Subject=${s}&Message=${m}&secureCaptcha=${c}&SendButton=Send+e-mail" \
| grep -E '"boxRed"|"boxGreen"' \
| sed -E 's/.*boxRed">//' \
| sed -E 's/.*boxGreen">//' \
| sed -E 's/<\/div>.*//')
[[ "$s" =~ \[[0-9]+\] ]] && print_error "$s"
print_info "$s"
}
main() {
set_args "$@"
set_command
set_var
check_var
account_login
send_mail
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
trap cleanup INT
trap cleanup EXIT
main "$@"
fi