aboutsummaryrefslogtreecommitdiffhomepage
path: root/snippets
diff options
context:
space:
mode:
Diffstat (limited to 'snippets')
l---------snippets/snippets1
-rw-r--r--snippets/use_curl_send_mail38
-rw-r--r--snippets/use_openssl_send_mail36
3 files changed, 74 insertions, 1 deletions
diff --git a/snippets/snippets b/snippets/snippets
deleted file mode 120000
index a05fd8e..0000000
--- a/snippets/snippets
+++ /dev/null
@@ -1 +0,0 @@
1/home/pham/helper/snippets \ No newline at end of file
diff --git a/snippets/use_curl_send_mail b/snippets/use_curl_send_mail
new file mode 100644
index 0000000..70eeb7c
--- /dev/null
+++ b/snippets/use_curl_send_mail
@@ -0,0 +1,38 @@
1#! /usr/bin/env bash
2
3# User input
4sender=typebrook@gmail.com
5receiver=typebrook@gmail.com
6gapp=$(pass google/imap_for_typebrook)
7sub="$(date) test mail from curl"
8body="$(echo -e 'just\na\ntest\nmail')"
9
10# check for provided attachment file as a positional parameter
11# -z indicating an empty positional parameter
12# attachment doesn't exist condition
13if [ -z "$1" ]; then
14
15 # curl command for accessing the smtp server
16
17 curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
18 --mail-from $sender \
19 --mail-rcpt $receiver --user $sender:$gapp \
20 -T <(echo -e "From: ${sender}
21To: ${receiver}
22Subject: ${sub}
23
24 ${body}")
25
26# attachment exists condition
27else
28 file="$1" # set file as the 1st positional parameter
29
30 # MIME type for multiple type of input file extensions
31 MIMEType=$(file --mime-type "$file" | sed 's/.*: //')
32 curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
33 --mail-from $sender \
34 --mail-rcpt $receiver --user $sender:$gapp \
35 -H "Subject: $sub" -H "From: $sender" -H "To: $receiver" -F \
36 '=(;type=multipart/mixed' -F "=$body;type=text/plain" -F \
37 "file=@$file;type=$MIMEType;encoder=base64" -F '=)'
38fi
diff --git a/snippets/use_openssl_send_mail b/snippets/use_openssl_send_mail
new file mode 100644
index 0000000..fabaca3
--- /dev/null
+++ b/snippets/use_openssl_send_mail
@@ -0,0 +1,36 @@
1#!/bin/bash
2
3# ref: https://stackoverflow.com/questions/9998710/is-it-possible-to-send-mails-by-bash-script-via-smtp
4# Use "host -t mx host" to find out mail server
5
6set -e
7
8MAIL_SERVER=smtp.gmail.com
9PORT=465
10MAIL_FROM=typebrook@gmail.com
11RCPT_TO=typebrook@gmail.com
12
13send_message_line_by_line() {
14 while read line; do
15 echo $line >/dev/tty
16 echo $line
17 sleep 1
18 done | openssl s_client -connect $MAIL_SERVER:$PORT -crlf
19}
20
21# 1. openssl do RENEGOTIATING when input starts with R, so use "rcpt to:" instead of "RCPT TO:"
22# 2. End mail with an empty line, and followed by '.' and 'QUIT'
23cat <<MAILEND | send_message_line_by_line
24HELO $MAIL_SERVER
25AUTH LOGIN
26$(echo $MAIL_FROM | base64)
27$(pass google/imap_for_typebrook | base64)
28MAIL FROM: <$MAIL_FROM>
29rcpt to: <$RCPT_TO>
30DATA
31Subject: $(date) test mail from bash
32Another test mail
33
34.
35QUIT
36MAILEND