From 3526eb6d756e3c37190814eca2e22ce9f7d9f1de Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 28 Jun 2024 01:24:13 +0800 Subject: Update --- snippets/snippets | 1 - snippets/use_curl_send_mail | 38 ++++++++++++++++++++++++++++++++++++++ snippets/use_openssl_send_mail | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) delete mode 120000 snippets/snippets create mode 100644 snippets/use_curl_send_mail create mode 100644 snippets/use_openssl_send_mail (limited to 'snippets') diff --git a/snippets/snippets b/snippets/snippets deleted file mode 120000 index a05fd8e..0000000 --- a/snippets/snippets +++ /dev/null @@ -1 +0,0 @@ -/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 @@ +#! /usr/bin/env bash + +# User input +sender=typebrook@gmail.com +receiver=typebrook@gmail.com +gapp=$(pass google/imap_for_typebrook) +sub="$(date) test mail from curl" +body="$(echo -e 'just\na\ntest\nmail')" + +# check for provided attachment file as a positional parameter +# -z indicating an empty positional parameter +# attachment doesn't exist condition +if [ -z "$1" ]; then + + # curl command for accessing the smtp server + + curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \ + --mail-from $sender \ + --mail-rcpt $receiver --user $sender:$gapp \ + -T <(echo -e "From: ${sender} +To: ${receiver} +Subject: ${sub} + + ${body}") + +# attachment exists condition +else + file="$1" # set file as the 1st positional parameter + + # MIME type for multiple type of input file extensions + MIMEType=$(file --mime-type "$file" | sed 's/.*: //') + curl -s --url 'smtps://smtp.gmail.com:465' --ssl-reqd \ + --mail-from $sender \ + --mail-rcpt $receiver --user $sender:$gapp \ + -H "Subject: $sub" -H "From: $sender" -H "To: $receiver" -F \ + '=(;type=multipart/mixed' -F "=$body;type=text/plain" -F \ + "file=@$file;type=$MIMEType;encoder=base64" -F '=)' +fi 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 @@ +#!/bin/bash + +# ref: https://stackoverflow.com/questions/9998710/is-it-possible-to-send-mails-by-bash-script-via-smtp +# Use "host -t mx host" to find out mail server + +set -e + +MAIL_SERVER=smtp.gmail.com +PORT=465 +MAIL_FROM=typebrook@gmail.com +RCPT_TO=typebrook@gmail.com + +send_message_line_by_line() { + while read line; do + echo $line >/dev/tty + echo $line + sleep 1 + done | openssl s_client -connect $MAIL_SERVER:$PORT -crlf +} + +# 1. openssl do RENEGOTIATING when input starts with R, so use "rcpt to:" instead of "RCPT TO:" +# 2. End mail with an empty line, and followed by '.' and 'QUIT' +cat < +rcpt to: <$RCPT_TO> +DATA +Subject: $(date) test mail from bash +Another test mail + +. +QUIT +MAILEND -- cgit v1.2.3-70-g09d2