-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/email
Copy pathMore file actions
executable file
·76 lines (61 loc) · 2.36 KB
/email
File metadata and controls
executable file
·76 lines (61 loc) · 2.36 KB
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
#!/bin/bash
# DEPENDENCIES: pass, parse*, jq, txtin*, curl
# where * are personal scripts: www.github.com/algorist-ahmad/scripts
# PARAMETERS: -to, -sub, -msg
# SMTP server settings for Gmail
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT="587"
SMTP_USERNAME="$EMAIL"
SMTP_PASSWORD=$(pass $EMAIL_PASSWORD)
HTML_FILE="$(parse $@ | jq -r '.args.parsed.html')" # plaintext is the default
# email content
TO="$(parse $@ | jq -r '.args.parsed.to')"
SUBJECT="$(parse $@ | jq -r '.args.parsed.sub')"
BODY="$(parse $@ | jq -r '.args.parsed.msg')"
FOOTNOTE="*********************************************************\nsent via curl by ~/bin/email on $(date)"
prompt_user() {
TO=$EMAIL ; echo "TO: $EMAIL"
SUBJECT=$(txtin -p 'SUBJECT: ')
BODY=$(txtin -m -p 'COMPOSE MESSAGE, TERMINATE WITH EOF:\n')
}
send_as_plaintext() {
[[ "$TO" =~ ^(null|true)$ ]] && >&2 echo 'ERROR: NO RECIPIENT (-to)' && exit 1
[[ "$BODY" =~ ^(null|true)$ ]] && >&2 echo 'ERROR: EMPTY MESSAGE (-msg)' && exit 2
[[ "$SUBJECT" =~ ^(''|null|true)$ ]] && >&2 echo 'WARNING: NO SUBJECT' && sleep 1 && SUBJECT=$(date)
# Construct the email message
MESSAGE="Subject: $SUBJECT\n\n$BODY\n\n\n\n\n\n$FOOTNOTE"
# Send the email using Curl
curl --show-error \
--url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--ssl-reqd \
--mail-from "$SMTP_USERNAME" \
--mail-rcpt "$TO" \
--user "$SMTP_USERNAME:$SMTP_PASSWORD" \
--tlsv1.2 \
-T <(echo -e "$MESSAGE")
echo "Email sent to $TO"
}
send_as_multipart() {
[[ "$TO" =~ ^(null|true)$ ]] && >&2 echo 'ERROR: NO RECIPIENT (-to)' && exit 1
[[ "$BODY" =~ ^(null|true)$ ]] && >&2 echo 'ERROR: EMPTY MESSAGE (-msg)' && exit 2
[[ "$SUBJECT" =~ ^(''|null|true)$ ]] && >&2 echo 'WARNING: NO SUBJECT' && sleep 1 && SUBJECT=$(date)
# Construct the email message
MESSAGE="Subject: $SUBJECT\n\n$BODY\n\n\n\n\n\n$FOOTNOTE"
# Send the email using Curl, changed to --ssl from --ssl-reqd (what is that?)
curl --show-error \
--url "smtp://$SMTP_SERVER:$SMTP_PORT" \
--ssl-reqd \
--mail-from "$SMTP_USERNAME" \
--mail-rcpt "$TO" \
--user "$SMTP_USERNAME:$SMTP_PASSWORD" \
--tlsv1.2 \
--upload-file "$BODY"
echo "Email sent to $TO"
}
[[ $# -eq 0 ]] && prompt_user || parse $@ | jq -c '.'
if [[ -n "$HTML_MODE" ]]; then
echo "SPECIAL MODE: HTML"
send_as_multipart
else
send_as_plaintext
fi