Skip to content

Instantly share code, notes, and snippets.

@shidaxi
Created May 19, 2017 13:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shidaxi/227b805a87e558cff0fb6298c62f2a15 to your computer and use it in GitHub Desktop.
Save shidaxi/227b805a87e558cff0fb6298c62f2a15 to your computer and use it in GitHub Desktop.
use nc(netcat) to send email via smtp server
#!/bin/bash
from=$3
to=$4
function err_exit { echo -e 1>&2; exit 1; }
if [ $# -ne 4 ]; then
echo -e "\n Usage error!"
echo " This script requires four arguments:"
echo " 1. recepient mail server"
echo " 2. port (typically 25 or 465)"
echo " 3. mail from (e.g. from@example.com)"
echo " 4. mail to (e.g. to@example.com)"
exit 1
fi
# create message
function mail_input {
echo "ehlo $(hostname -f)"
echo "MAIL FROM: <$from>"
echo "RCPT TO: <$to>"
echo "DATA"
echo "From: <$from>"
echo "To: <$to>"
echo "Subject: Testing one two three"
echo "This is only a test. Please do not panic. If this works, then all is well, else all is not well."
echo "In closing, Lorem ipsum dolor sit amet, consectetur adipiscing elit."
echo "."
echo "quit"
}
mail_input | nc $1 $2 || err_exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment