Skip to content

Instantly share code, notes, and snippets.

@rjaeckel
Created April 18, 2016 12:40
Show Gist options
  • Save rjaeckel/24faeaef8c1810dfeaabe71fd560e58d to your computer and use it in GitHub Desktop.
Save rjaeckel/24faeaef8c1810dfeaabe71fd560e58d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# smtp email address verification
# using dig and netcat
#
# authored by robert.jaeckel@itz.uni-halle.de
#
checkaddr=$1
fromaddr=$2
netcat=$(which netcat)
fromhost=$(hostname)
dig=$(which dig)
domainname=$(sed -r 's/^(.+)@(.+)$/\2/' <<<$checkaddr)
mxrecord=`$dig +noall +answer mx $domainname|awk -e '{print $6}'|sed -r 's/(.+)\.$/\1/'|head -n1`
if [ -z $mxrecord ]; then mxrecord=$domainname; fi
rm -rf cmd resp
mkfifo cmd resp
echo "$netcat $mxrecord smtp" >&2
$netcat $mxrecord smtp <cmd >resp &
exec 3>cmd
while read cmd; do
#echo "awaiting response..." >&2
read -t 10 <resp || { echo "timeout.">&2 ; exit 2; } #quit on read error e.g. by timeout
echo "< $REPLY" >&2
echo "> $cmd" >&2
echo $cmd >cmd
#echo "sent cmd" >&2
done <<EOF
HELO $fromhost
MAIL FROM: <$fromaddr>
RCPT TO: <$checkaddr>
QUIT
EOF
exec 3>&-
rm cmd resp
echo $REPLY
code=$(echo $REPLY | awk -e '{ print $1 }')
if [ "$code" = "250" ]; then exit 0; else exit 1; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment