Skip to content

Instantly share code, notes, and snippets.

@hydrastro
Last active October 22, 2021 13:18
Show Gist options
  • Select an option

  • Save hydrastro/5ccfc0dc87a8bb3c5ec9062a3cba5bf6 to your computer and use it in GitHub Desktop.

Select an option

Save hydrastro/5ccfc0dc87a8bb3c5ec9062a3cba5bf6 to your computer and use it in GitHub Desktop.
Bash SMTP(/S) authentication and mailsending script
#!/bin/bash
smtp_address=""
smtp_username=""
smtp_password=""
smtp_server=""
smtp_port=587
use_starttls=true
send_mail() {
email_recipient="$1"
email_body="$2"
email_subject="$3"
{ \
sleep 1; \
printf "HELO %s\n" "$smtp_server"; \
sleep 1; \
printf "AUTH LOGIN\n"; \
sleep 1; \
printf "%s" "$smtp_username" | base64 -w 0; \
printf "\n"; \
sleep 1; \
printf "%s" "$smtp_password" | base64 -w 0; \
printf "\n"; \
sleep 1;
printf "MAIL FROM: <%s>\n" "$smtp_address"; \
sleep 1; \
printf "RCPT TO: <%s>\n" "$email_recipient"; \
sleep 1; \
printf "DATA\n"; \
sleep 1; \
printf "Subject: %s\n" "$email_subject"; \
printf "%s\n" "$email_body"; \
sleep 1; \
printf "\n.\n";
sleep 1; \
printf "QUIT\n"; \
} | \
if ${use_starttls} ; then
openssl s_client -starttls smtp -connect "${smtp_server}:${smtp_port}" \
-quiet
else
telnet "$smtp_server" "$smtp_port"
fi
}
# example usages:
# unmounted disk notify
disk_mountpoint=""
if ! grep -qs "$disk_mountpoint" /proc/mounts; then
send_mail "[email protected]" "Unmounted Drive" \
"Unmounted drive detected."
fi
# server down notify
server_address=""
server_port=22
if ! torify nc -z "$server_address" "$server_port" > /dev/null; then
send_mail "[email protected]" \
"Backup Server Down" "The backup server is down."
fi
@hydrastro
Copy link
Author

It just notifies me when a server has a power outage and needs my intervention for decrypting and mounting its drives.

@hydrastro
Copy link
Author

hydrastro commented Apr 26, 2021

It just notifies me when a server has a power outage and needs my intervention for decrypting and mounting its drives.

or when a server is unreachable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment