diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-06-28 01:24:13 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-06-28 01:24:13 +0800 |
| commit | 3526eb6d756e3c37190814eca2e22ce9f7d9f1de (patch) | |
| tree | f334c6ef0a4b77c40980eee7a5fdf07899405069 /snippets | |
| parent | e58f70b411ae935fcd32f00d9e3983381fdc0294 (diff) | |
Update
Diffstat (limited to 'snippets')
| l--------- | snippets/snippets | 1 | ||||
| -rw-r--r-- | snippets/use_curl_send_mail | 38 | ||||
| -rw-r--r-- | snippets/use_openssl_send_mail | 36 |
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 | ||
| 4 | sender=typebrook@gmail.com | ||
| 5 | receiver=typebrook@gmail.com | ||
| 6 | gapp=$(pass google/imap_for_typebrook) | ||
| 7 | sub="$(date) test mail from curl" | ||
| 8 | body="$(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 | ||
| 13 | if [ -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} | ||
| 21 | To: ${receiver} | ||
| 22 | Subject: ${sub} | ||
| 23 | |||
| 24 | ${body}") | ||
| 25 | |||
| 26 | # attachment exists condition | ||
| 27 | else | ||
| 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 '=)' | ||
| 38 | 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 @@ | |||
| 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 | |||
| 6 | set -e | ||
| 7 | |||
| 8 | MAIL_SERVER=smtp.gmail.com | ||
| 9 | PORT=465 | ||
| 10 | MAIL_FROM=typebrook@gmail.com | ||
| 11 | RCPT_TO=typebrook@gmail.com | ||
| 12 | |||
| 13 | send_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' | ||
| 23 | cat <<MAILEND | send_message_line_by_line | ||
| 24 | HELO $MAIL_SERVER | ||
| 25 | AUTH LOGIN | ||
| 26 | $(echo $MAIL_FROM | base64) | ||
| 27 | $(pass google/imap_for_typebrook | base64) | ||
| 28 | MAIL FROM: <$MAIL_FROM> | ||
| 29 | rcpt to: <$RCPT_TO> | ||
| 30 | DATA | ||
| 31 | Subject: $(date) test mail from bash | ||
| 32 | Another test mail | ||
| 33 | |||
| 34 | . | ||
| 35 | QUIT | ||
| 36 | MAILEND | ||