Script for testing SMTP with Telnet

There are times when we, as email system administrators, may have issues with mail flow between our GWIAs and external systems. A common troubleshooting step is to go back to basics and use Telnet to test the SMTP connectivity and send a test email. This troubleshooting step can prove invaluable and assist us in diagnosing where the problem is. To make my life a little easier I wrote a script which [almost] automates this task for me, negating the need to remember the exact commands and syntax. I have found this useful and thought I'd share here.

Disclaimer:  This was created out of my own necessity.  Scripting does not come naturally to me, so this won't look pretty.

On your GWIA server, in an appropriate directory, create a file. I call it

Copy this code into the file:


read -p "Enter Recipient's Mail Server: " server
read -p "Enter SMTP Port: " port
read -p "Enter Your Email Address: " from
read -p "Enter Recipient's Email Address: " to

# 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 SMTP Mail"
echo "This is only a test. Please do not panic. If this works, then all is well, else all is not well."
echo "."
echo "quit"

mail_input | netcat $server $port || err_exit

Save the file and mark it executable
 chmod  x 

Edit the subject line and text to be sent in the email to suit your needs. When you run the script it will prompt you for the necessary information to complete the task at hand.



How To-Best Practice
Comment List