Skip to content

Instantly share code, notes, and snippets.

@TerryMooreII
Created September 24, 2012 00:32
Show Gist options
  • Save TerryMooreII/3773579 to your computer and use it in GitHub Desktop.
Save TerryMooreII/3773579 to your computer and use it in GitHub Desktop.
Sending SMTP Commands with /dev/tcp
#!/bin/bash
# Written by: Terry Moore
# Created on Date: 2009-10-12
# Test mail relay from a Linux database server
# Version 0.2
# Last update 2009-10-13
#Body of Email
DATA="The test message has been sent"
#Subject of Email
SUBJECT="Mail Relay Test"
######### Header ##########
echo
echo
echo
echo "***************************************"
echo "* *"
echo "* Mail Relay Test App *"
echo "* *"
echo "***************************************"
echo
echo "Press control+c at any time to cancel"
echo "Please answer all of the following questions:"
echo
##### Get Mail Server #######
LOOP=0
while [ $LOOP -ne 1 ]
do
echo -n "Enter Mail Server Name: " ; read MAILSERVER;
if [ "$MAILSERVER" != '' ] ; then
LOOP=1
fi
done
###### GET PORT ##########
echo -n "Enter Port: [typically 25]: "; read PORT;
if [ "$PORT" = '' ] ; then
LOOP=1
PORT="25"
fi
###### GET MAIL FROM ##########
echo -n "Enter Mail From: [support@email.com]" ; read MAILFROM
if [ "$MAILFROM" = '' ] ; then
MAILFROM="support@email.com"
LOOP=0
fi
###### GET MAIL TO ##########
LOOP=0
while [ $LOOP -ne 1 ]
do
echo -n "Enter Mail To: " ; read MAILTO
if [ "$MAILTO" != '' ] ; then
LOOP=1
fi
done
#### SEND MAIL via RAW TCP #######
echo
echo "Connecting to $MAILSERVER on Port $PORT";
echo "Please wait ... "
echo
exec 3<>/dev/tcp/$MAILSERVER/$PORT
if [ $? -ne 0 ] ; then
echo
echo "ERROR: Cannot connect to the Mail Server";
echo "Please check the servername and/or the port number"
exit
fi
echo -en "HELO mail.email.com\r\n" >&3
echo -en "MAIL FROM:$MAILFROM\r\n" >&3
echo -en "RCPT TO:$MAILTO\r\n" >&3
echo -en "DATA\r\n" >&3
echo -en "Subject: $SUBJECT\r\n\r\n" >&3
echo -en "$DATA\r\n" >&3
echo -en ".\r\n" >&3
echo -en "QUIT\r\n" >&3
cat <&3
echo
echo
echo "Check the above output for errors"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment