Testing Email Server Response
A little while ago I ran into a situation when connections to the relay email server would occasionally time out. The issue seemed sporadic resulting in a controversy regarding its source. The perimeter email server would receive the emails, run a preliminary virus scan and forward the emails to the relay. From time to time the perimeter email server would log the “Lost connection while receiving initial greeting” error while trying to contact the relay.
Below is a simple script that uses the “expect” command to talk to the mail server and provides some additional information about the responses from the server that can be used for troubleshooting. Save the script to an executable script file and run it with the “time” command.
#!/bin/bash d=$(date) expect -c "spawn nc -vt <mail_server> 25;\ expect \"*succeeded*\";\ send \"helo <sender_domain>\r\";\ expect \"250 <mail_server>*\";\ send \"mail from: <sender_address>\r\";\ expect \"*Sender ok*\";\ send \"rcpt to: <recipient_address>\r\";\ expect \"*Recipient ok*\";\ send \"data\r\";\ expect \"*Enter mail*\";\ send \"From: 'Sender Name' <sender_address>\r\";\ send \"To: 'Recipient Name' <recipient_address>\r\";\ send \"Subject: test email\r\";\ send \"Date: ${d}\r\";\ send \"This is a test email\r\";\ send \".\r\";\ expect \"*Message accepted for delivery*\";\ send \"\r\";\ send \"quit\r\";\ expect \"*Closing connection*\";\ exit"