#!/bin/sh
                                                                                
# smtptest2, ver. 2.0
# (c) 2003 Jan ONDREJ (SAL) <ondrejj@salstar.sk>

# usage:
#   ./smtptest2 to virus [port]
# example:
#   ./smtptest2 root@localhost virus
# If you want to define mail from: ..., use:
# MAILFROM=user@host ./smtptest2 ...

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

if [ -z "$MAILFROM" ]; then
  MAILFROM="$USER@`hostname`"
fi

if [ "$3" ]; then
  PORT=$3
else
  PORT=25
fi

if [ -z "$SMTPHOST" ]; then
  SMTPHOST1=`echo $1 | cut -d@ -f2`
  SMTPHOST=`host -t mx $SMTPHOST1 | sort -n +5 | head -1 | cut -d" " -f7`
  if [ -z "$SMTPHOST" ]; then
    SMTPHOST=`host $SMTPHOST1 | cut -d" " -f4`
  fi
fi

(
  echo "helo `hostname`"
  echo "mail from:$MAILFROM"
  echo "rcpt to:$1"
  echo "data"
  sleep 1s
  cat $2
  echo "."
  sleep 1s
  echo "quit"
) | nc $SMTPHOST $PORT
