Script for testing SMTP with Telnet

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.



Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2018-01-02 17:17
Updated by:
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.